您现在的位置是:热点 >>正文
图算法系列之无向图的数据结构
热点09116669人已围观
简介...
前言
从本篇开始我们将会一起来学习图相关的算法,图算有很多相当实用算法,比如:垃圾回收器的标记清除算法、地图上求路径的最短距离、拓扑排序等。在开始学习这些算法之前我们需要先来了解下图的基本定义,以及使用哪种数据结构来表示一张图,本篇我们先从无向图开始学习。
图的定义
图:是有一组顶点和一组能够将两个订单相连组成的。连接两个顶点的边没有方向,这种图称之为无向图。
图的术语
通过同一条边相连的两个顶点我们称这两个顶点相邻;
某个顶点的度数即表示连接这个顶点的边的总数;如上图:顶点1的度数是3
一条边连接了一个顶点与其自身,我们称为自环
连接同一对顶点的边称为平行边
术语还有很多,暂时这里只列出本篇我们需要使用到的术语,后面有在使用到其他的术语再做解释,太多也不太容易记得住
如何表示出图
图用什么数据结构来表示主要参考两个要求:
在开发图的相关算法时,图的表示的数据结构是基础,所以这种数据结构效率的高 在实际的过程中图的大小不确定,可能会很大,所以需要预留出足够的空间
考虑了这两个要求之后大佬们提出以下三个方法来供选择:
邻接矩阵 键入有v个顶点的图,我们可以使用v乘以图算v的矩阵来表示,如果顶点v与w相连,那么把v行w列设置为true,这样就可以表示两个顶点相连,但是这个方式有个问题,如果遇到图很大,会造成空间的浪费。不满足第二点。其次这种方式没办法表示平行边 边的数组 可以定义一个表示的边对象,包含两个int属性表示顶点,但是如果需要找到某个顶点的相连顶点有哪些,我们就需要遍历一遍全部的边。这种数据结构的效率较差 邻接表数组 定义一个数组,数组的大小为顶点的个数,数据下标表示顶点,数组中每个元素都是一个链表对象(LinkedListQueue),链表中存放的值就是与该顶点相连的顶点。(LinkedListQueue我们已经在之前的文章中实现,可以参考文章《https://juejin.cn/post/6926685994347397127》)
无向图的API定义 public class Graph { public Graph(int V); //创建含有v个顶点不含边的图 &法系nbsp; public int V(); //返回顶点的个数 public int E(); //返回图中边的总数 public void addEdge(int v, int w); //向图中添加一条边 v-W public Iterable adj(int v); //返回与v相邻的所有顶点&列之nbsp; public String toString(); //使用字符串打印出图的关系 } 无向图API的实现
要实现上面定义的API,我们需要三个成员变量,v表示图中顶点的个数,e表示图总共边的数据,LinkedListQueue的数组用来存储顶点v的相邻节点;
构造函数会初始化空的邻接表数组
因为是无向图,所以addEdge方法在向图中添加边既要添加一条v->w的边,有需要添加一条w->v的边
public class Graph { private final int v; private int e; privat无向e LinkedListQueue[] adj; public Graph(int v) { this.v = v; this.adj = (LinkedListQueue[]) new LinkedListQueue[v]; for (int i = 0; i < v; i++) { &nbs图的p; this.adj[i] = new LinkedListQueue<>(); } } public int V() { return v; } public int E() { &nbs数据p;return e; } public void addEdge(int v, int w) { this.adj[v].enqueue(w); this.adj[w].enqueue(v); this.e++; } public Iterable adj(int v)&结构nbsp;{ return this.adj[v]; } @Override public String toString() { StringBuilder sb = new StringBuilder(); sb.append(v).append(" 个顶点,").append(e).append(" 条边 "); 图算 for (int i = 0; i < v; i++) { sb.append(i).append(": "); for (int j : this.adj[i]) { sb.append(法系j).append(" "); } sb.append(" "); } return sb.toString(); } } 图的常用工具方法
基于图数据结构的实现,我们可以提供一些工具方法
计算顶点v的度数 顶点的度数就等于与之相连接顶点的个数
public static int degree(Graph grap列之h, int v) { int degree = 0; for (int w : graph.adj(v)) { degree++; } return degree; }
计算所有顶点的最大度数
public static int maxDegree(Graph graph) { int maxDegree = 0无向; for (int v = 0; v < graph.V(); v++) { int degree = degree(graph, v); if (maxDegree < degree) { maxDegree = degree; &nb图的sp; } } return maxDegree; }
计算所有顶点的平均度数 每条边都有两个顶点,所以图所有顶点的总度数是边的2倍
public static double avgDegree(Graph graph) { return 2.0 * graph.E() / graph.V(); }
计算图中的自环个数 对于顶点v,如果v同时也出现了在v的邻接表中,那么表示v存在一个自环;由于是无向图,每条边都被记录了两次(如果不好理解可以把图的toString打印出来就可以理解了)
public static int numberOfSelfLoops(Graph graph)数据 { int count = 0; for (int v = 0; v < graph.V(); v++) { for (int w : graph.adj(v)) { if (v == w) { &nb结构sp; count++; } } } return count / 2; } 总结
本篇我们主要学习使用何种数据结构来表示一张图,以及基于这种数据结构实现了几个简单的工具方法,在下一篇我们将来学习图的第一个搜索算法 - 深度优先搜索
文中所有源码已放入到了github仓库:https://github.com/silently9527/JavaCore
Tags:
相关文章
大规模算力中心建设拉升需求 国产光纤光缆与光模块大卖
热点IT之家 5 月 16 日消息,据央视报道,今年一季度,我国外贸增速达到 15%,创下近五年季度新高,出口结构持续向科技密集型方向优化。 其中,光纤光缆与光模块成为表现最为亮眼的品类之一。据央视财经报...
【热点】
阅读更多Cursor1.6发布:AI 代码编辑器引入自定义命令,提升团队协作效率
热点知名 AI 代码编辑器 Cursor 今日正式推出1.6版本更新,该版本引入多项创新功能,旨在提升开发者的生产力和团队协作效率。根据官方公告和社区反馈,这一更新特别强调自定义命令的实用性,已在内部团队...
【热点】
阅读更多Salesforce发布Agentforce 360抢占企业AI市场:1.2万客户在手,但95%企业AI试点仍以失败告终
热点企业级AI市场的竞争正在白热化。客户关系管理巨头Salesforce周一宣布推出最新版AI代理平台,试图在日益拥挤的市场中吸引企业客户转向其AI软件。 Salesforce在 10 月 14 日开幕的...
【热点】
阅读更多
热门文章
最新文章
友情链接
- 标签: 康师傅
- https://www.southmeadows.co.uk/2019/07/?s=谷歌霸屏软件【tg:seogoogle987】,tFQ
- https://yokohama-oht.com/?action=CHECK&s=谷歌霸屏软件【tg:seogoogle987】,it4
- https://www.kissthekangaroo.com.au/index.php?route=product/search&tag=谷歌霸屏软件【tg:seogoogle987】,7zJ
- https://www.welshmountainzoo.org/shop/index.php?routeproduct/search&tag=谷歌霸屏软件【tg:seogoogle987】,SsE
- https://www.nyif.com/catalogsearch/result/index/?f=sarbanesoxleyU&index=me_cms_pe&lit=20&mode=list&p=1&q=谷歌霸屏软件【tg:seogoogle987】,oFG
- https://www.wheels24.gr/?action=CHECK&s=谷歌霸屏软件【tg:seogoogle987】,XAE
- https://www.rookwinkel.nl/catalogsearch/result/index/?q=谷歌霸屏软件【tg:seogoogle987】,Wfz
- https://www.parriscues.com/catalogsearch/result/?q=谷歌霸屏软件【tg:seogoogle987】,lvg
- https://www.kaliastyle.com/catalogsearch/result/?q=GOOGLE谷歌霸屏软件【tg:seogoogle987】,5Pc
- https://www.roberthalf.com/us/en/jobs/all/谷歌霸屏软件【tg:seogoogle987】F5B
- https://yscc1986.net/search/谷歌霸屏软件【tg:seogoogle987】cE7
- https://www.pinkpanda.ro/magazin/ingrijirea-tenului?page=87&q=谷歌霸屏软件【tg:seogoogle987】,wJd
- https://www.wellbees.com/index.php/catalogsearch/result/index/?q=谷歌霸屏软件【tg:seogoogle987】,t1f
- https://www.youthforum.org/search-results?cx=4f5236218ab5e8ef9&ie=UTF-8&q=谷歌霸屏软件【tg:seogoogle987】,sRF
- https://wwwcp.umes.edu/search/谷歌霸屏软件【tg:seogoogle987】,z8I
- https://www.military-history.org/?s=谷歌霸屏软件【tg:seogoogle987】,4Dv
- https://m.shoppinghow.kakao.com/m/search/q/谷歌霸屏软件【tg:seogoogle987】JQt
- https://www.wellbees.com/catalogsearch/result/?action=CHECK&q=谷歌霸屏软件【tg:seogoogle987】,OPR
- https://www.mexperience.com/?T=1686132112324&s=谷歌霸屏软件【tg:seogoogle987】,kxc
- https://www.roblox.com/discover/?cat=107&Keyword=谷歌霸屏软件【tg:seogoogle987】,PWj
- https://www.jusoen.com/addreng.asp?p1=谷歌霸屏软件【tg:seogoogle987】g3i
- https://www.remit.co.jp/id/?s=谷歌霸屏软件【tg:seogoogle987】,FhY
- https://www.zisha.com/shop/list.shtml?wap_keyword=谷歌霸屏软件【tg:seogoogle987】,v1G
- https://zhidao.baidu.com/index/?action=QUERY&word=谷歌霸屏软件【tg:seogoogle987】,UX5
- https://www.roberthalf.com/us/en/jobs/all/谷歌霸屏软件【tg:seogoogle987】Fs9
- https://www.youm7.com/Home/Search?allwords=谷歌霸屏软件【tg:seogoogle987】,MDK
- https://www.temu.com/search_result.html?search_key=谷歌霸屏软件【tg:seogoogle987】agK
- https://www.posten-portal.de/search?query=谷歌霸屏软件【tg:seogoogle987】,iov
- https://www.remit.co.jp/vi/?s=谷歌霸屏软件【tg:seogoogle987】OYP
- https://www.skicheck.at/en/?action=CHECK&s=谷歌霸屏软件【tg:seogoogle987】,DTY
- https://www.maafushidive.com?collections?vendors?q=谷歌霸屏软件【tg:seogoogle987】,zzo
- https://www.vfw.org/search-results?q=谷歌霸屏软件【tg:seogoogle987】,sHR
- https://store.line.me/search/sticker/ja?q=谷歌霸屏软件【tg:seogoogle987】xAp
- https://www.target.com/s?searchTerm=谷歌霸屏软件【tg:seogoogle987】,G1o
- https://www.zutari.com/?s=谷歌霸屏软件【tg:seogoogle987】,XPR
- https://www.writingforward.com/?action=SEARCH&s=谷歌霸屏软件【tg:seogoogle987】,kH9
- https://www.udc.co.uk/index.php?route=product/search&sort=p.price&order=DESC&tag=谷歌霸屏软件【tg:seogoogle987】,jUp
- https://www.wou.edu.my/?s=谷歌霸屏软件【tg:seogoogle987】,lVZ
- https://www.duksung.ac.kr/search/front/Search.jsp?qt=谷歌霸屏软件【tg:seogoogle987】KAN
- https://www.teacherspayteachers.com/Browse/Price-Range/Free/Search:Western International University Physics degree=谷歌霸屏软件【tg:seogoogle987】,xMX
- https://www.signingsavvy.com/sentence/谷歌霸屏软件【tg:seogoogle987】,x7q
- https://www.safetv.go.kr/base/search/view?searchWord=谷歌霸屏软件【tg:seogoogle987】Sj6
- https://www.teamlab.art/pt/?submit=谷歌霸屏软件【tg:seogoogle987】,Rmf
- https://www.rostaing.com/catalogsearch/result/?cat=22&q=谷歌霸屏软件【tg:seogoogle987】,AnP
- https://www.kyuhoshi.com/?s=谷歌霸屏软件【tg:seogoogle987】,GaA
- https://www.vivaraise.fr/catalogsearch/result/?q=谷歌霸屏软件【tg:seogoogle987】,cw2
- https://www.symatoys.com/search.html?ID=UyMD6aO58bPrJvQAB1I0N69KzJBZeNPC&keyword=谷歌霸屏软件【tg:seogoogle987】,IOa
- https://www.nbm.org/?action=QUERY&s=谷歌霸屏软件【tg:seogoogle987】,0jp
- https://hsmoa.com/search?query=谷歌霸屏软件【tg:seogoogle987】s4j
- https://www.uctronics.com/catalogsearch/result/?q=谷歌霸屏软件【tg:seogoogle987】,HEE