1 / 45

Chapter 7: Graph

Data Structure and Algorithms. Chapter 7: Graph. 7.1 图的定义和术语 7.2 图的存储结构 7.2.1 数组表示法 7.2.2 邻接表 7.2.3 十字链表 7.2.4 邻接多重表 7.3 图的遍历 7.4 最小生成树 7.5 拓扑排序与关键路径 7.6 最短路径. 7.1 Definition of Graphs. Consists of vertices (顶点) and edge( 边) or arc ( 弧) which are pairs of vertices. G=(V,A)

thelma
Download Presentation

Chapter 7: Graph

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Data Structure and Algorithms Chapter 7: Graph

  2. 7.1 图的定义和术语 7.2 图的存储结构 7.2.1 数组表示法 7.2.2 邻接表 7.2.3 十字链表 7.2.4 邻接多重表 7.3 图的遍历 7.4 最小生成树 7.5 拓扑排序与关键路径 7.6 最短路径

  3. 7.1 Definition of Graphs Consists of vertices(顶点) and edge(边)or arc (弧) which are pairs of vertices. G=(V,A) V={Vertices } A={Edge/Arc}

  4. Beijing Tianjin Shanghai Shenzhen Guangzhou Graphs example –Airline • Vertices are cities • Edges are airlines

  5. Graphs example–Computer Network computer connection • Vertices are computers • Edges are pairs of computers to be connected together

  6. Graphs VS. Tree • General graphs differ from trees • need not have a root node • no implicit parent-child relationship • may be several (or no) paths from one vertex to another.

  7. Review of graph terminology-Undirected graphs(无向图) V2 V1 G=(V,A) V={V1, V2, V3, V4 ,V5} A={(V1, V4 ), (V1, V2), (V2, V3 ), (V2, V5 ), (V3, V4 ), (V3, V5 ) } V3 V4 V5

  8. Review of graph terminology- Directed graphs(有向图) A directed graph, or digraph, consists of: ·finite set of elements called vertices or nodes ·a finite set of directed arcs that connect pairs of vertices V1 V2 V1 G=(V,A) V={V1, V2, V3, V4 } A={<V1, V2>, <V1, V3 >,<V3, V4>, <V2, V4 > } V3 V4

  9. More about directed graphs • If there is an edge from v 1 to v 2 , then v 2 is adjacent to v 1 (but not necessarily vice versa). • A directed path starts at some vertex v 1 , goes to some vertex v 2 adjacent to v 1 , then goes to some vertex v 4 adjacent to v 2 , and so on. • Can replace any undirected graph with an equivalent directed graph by replacing edge undirected edge with directed edges going in both directions

  10. Beijing Tianjin 1300 900 Shanghai 780 1100 Shenzhen 1000 1100 Guangzhou Review of graph terminology - weighted graph(网) Weights are tickets fare

  11. Review of graph terminology-Path,Cycle…... • Two vertices are adjacentif there is an edge between them. • Apathstarts at some vertex v 1 , goes to some vertex v 2 adjacent to v 1 , then goes to some vertex v 3 adjacent to v 2 , and so on. • A cycleis a path that starts and ends at the same vertex. • A path that does not contain any cycles is a simple path. • A graph isconnectedif there is a path between any pair of vertices. • A graph iscompleteif there is an edge between any pair of vertices.

  12. V2 V1 V3 V4 V5 Review of graph terminology V1 and V2 are adjacent Path from V1 to V3 Circle V3-V2-V5 Simple path V4-V3-V5 Connected but not complete

  13. V1 V3 V2 V1 V4 Sparse graph(e<nlogn) V3 V2 V4 Review of graph terminology A complete graph is a graph in which there is an edge between each pair of vertices.  A digraph of n nodes has n*(n-1) arcs (edges). Complete undirected graph/digraph (n(n-1))/2=(4*(4-1))/2=6 n(n-1)=4*(4-1)=12 Dense graph

  14. V2 V1 V2 V1 V2 V3 V3 V4 V5 V5 Graph2 V2 V1 V3 V4 V5 Sub graphs of Undirected graph V1 Suppose there are two graph G=(V,{E}) and G’=(V’,{E’}), If V’ is included by V and E’ is included by E, then G’ is the subtree of G. ……

  15. V1 V2 V1 V3 V4 V1 V2 V3 V3 V4 V1 Graph1 V3 V4 Sub graphs of Digraph V1 ……

  16. V2 V1 V1 V2 V3 V4 V5 V3 V4 Graph2 Graph1 Review of graph terminology- Degree • Degree: TD(v) • Indegree: ID(v) • Outdegree: OD(v) • TD(v)=ID(v)+OD(v)

  17. B A C F B A J C D E L M F G H I J K D E L M G H K I Review of graph terminology -connected graph Connected Graph Connected Component(连通分量)

  18. V1 V3 V4 V1 V2 V3 V4 Graph1 Strong connected components (强连通分量) V2

  19. Connected component B A C F B A J C D E L M F G H I J K B A L M C F J L M Review of graph terminology-Sub Tree Sub tree

  20. Review of graph terminology-Sub Tree • 一个连通图的 生成树是一个极小连通子图,它含有图中的全部顶点,但只有足以构成一颗树的n-1条边。 • 一颗有n个顶点的生成树有且仅有n-1条边。

  21. FirstAdjVex PutVex V1 V2 GetVex NextAdjVex ADT graph InsertVex/InsertArc V3 V4 LocateVex Graph1 DeleteVex/DeleteArc Destroy Graph DFSTraverse Create Graph BFSTraverse ADT graph

  22. 7.2.1邻接矩阵—表示顶点间相联关系的矩阵 • 定义:设G=(V,E)是有n1个顶点的图,G的邻接矩阵A是具有以下性质的n阶方阵

  23. 1 2 3 4 5 1 1 0 1 1 0 1 2 5 0 0 1 0 0 2 1 3 0 0 0 1 0 4 0 0 1 0 0 3 4 5 0 0 0 0 0 Graph representation-Adjacency Matrix Use a two-dimensional integer/boolean array to store whether there is an edge from each vertex to each other vertex. Adjacency matrix representation for directed graph with vertices numbered 1, 2, . . . , n is the n X n matrix adj, in which the entry in row i and column j is 1 (or true) if vertex j is adjacent to vertex i (that is, if there is a directed arc from vertex i to vertex j ), and is 0 (or false) otherwise.  

  24. V2 V1 0 1 1 0 0 0 0 0 0 0 0 0 1 0 0 0 V1 V2 G1= V3 V4 V5 V3 V4 Graph2 Graph1 • 0 1 0 1 0 • 1 0 1 0 1 • 0 1 0 1 1 • 1 0 1 0 0 • 0 1 1 0 0 G2= Graph representation-Adjacency Matrix

  25. 特点: • 无向图的邻接矩阵对称,可压缩存储;有n个顶点的无向图需存储空间为n(n+1)/2 • 有向图邻接矩阵不一定对称;有n个顶点的有向图需存储空间为n² • 无向图中顶点Vi的度TD(Vi)是邻接矩阵A中第i行元素之和 • 有向图中, • 顶点Vi的出度是A中第i行元素之和 • 顶点Vi的入度是A中第i列元素之和 • 网络的邻接矩阵可定义为:

  26. Graph Representation - Weighted Graph (Net) For a weighted digraph,there is some “cost” or “weight” is associated with each arc. The cost of the arc from vertex i to vertex j is used instead of 1 in the adjacency matrix. Clearly, some non-existent cost (such as 0, or –1) must then be used to indicate when an edge does not exist.

  27. Graph Representation-Adjacency Matrices-Net • 5  7   •  4    •     9 •  5   6 •   5   3    1  5 V2 V1 3 4 8 V6 9 V3 6 7 1 V5 5 V4 5 Wij if <vi, vj> or (vi,vj) VR A[i][j]=  otherwise

  28. 7.2.2 邻接表 • 实现:为图中每个顶点建立一个单链表,第i个单链表中的结点表示依附于顶点Vi的边(有向图中指以Vi为尾的弧)

  29. data firstarc VNode adjvex nextarc info ArcNode Graph representation-Adjacency lists Typedef struct VNode { VertexType data; struct ArcNode *firstarc; } Vnode, AdjList[MAX_VERTEX_NUM] Typedef struct ArcNode { int adjvex; struct arcnode *nextarc; InfoType info; } ArcNode;

  30. adjvex nextarc info data firstarc V2 V1 0 V1 2 1 1 V2 V3 3 2 V3 V4 3 V4 0 0 V1 3 1 V2 1 V2 4 2 0 V1 2 V3 4 3 1 2 0 3 V4 V3 4 V5 2 1 V4 V5 G2 Adjacency List G1

  31. 弧结点: typedef struct arcnode { int tailvex, headvex; //弧尾、弧头在表头数组中位置 struct arcnode *hlink;//指向弧头相同的下一条弧 struct arcnode *tlink; //指向弧尾相同的下一条弧 }AD; tailvex headvex hlink tlink 顶点结点: typedef struct dnode { int data; //存与顶点有关信息 struct arcnode *firstin;//指向以该顶点为弧头的第一个弧结点 struct arcnode *firstout; //指向以该顶点为弧尾的第一个弧结点 }DD; DD g[M]; //g[0]不用 data firstin firstout 7.2.3 有向图的十字链表表示法

  32. 1 2 1 3 1 a b 2 a b 3 c c d 3 1 3 4 d 4 4 3 4 1 4 2 ^ ^ ^ ^ ^ ^ ^ ^

  33. 边结点: typedef struct node { int mark; //标志域 int ivex, jvex; //该边依附的两个顶点在表头数组中位置 struct node *ilink, *jlink; //分别指向依附于ivex和jvex的下一条边 }JD; mark ivex ilink jvex jlink 顶点结点: typedef struct dnode { int data; //存与顶点有关的信息 struct node *firstedge; //指向第一条依附于该顶点的边 }DD; DD ga[M]; //ga[0]不用 data firstedge • 7.2.4 无向图的邻接多重表表示法

  34. 5 2 3 5 3 2 3 4 1 4 1 2 1 a 例 b 2 a b 3 c c 4 d d e 5 e ^ ^ ^ ^ ^

  35. 7.3 Graphs - Traversing • 图的遍历:从图中某一顶点出发访遍图中的其余顶点,且使每一个顶点仅被访问一次。 • 辅助数组 Visited[0..n-1] 避免同一个顶点被访问多次。Visited[0..n-1]的初始值置为False,一旦访问了顶点Vi,置visited[i]=True.

  36. Graphs - Traversing • Choices • Depth-First / Breadth-first • Depth First • Use an array of flags to mark“visited” nodes

  37. Graphs - Depth-first Traversing • 图的存储结构用邻接表。并设adjlist表示图graph的邻接表的表头指针向量,p是指针,指向当前顶点。visited是标志向量,visited(vi)=0表示未被访问。访问从v0开始,由于v0已访问,这时visited(v0)←1,再访问与v0邻接的 顶点。于是把v0的表头指针送给p,即p←adjlist(v0),当p所指的顶点未被访问时,则 访问它(即调用本过程)。否则p指向邻接表中的下一个结点。

  38. V1 V3 V2 V7 V4 V5 V1 V6 V2 V3 V2 V1 V4 V5 V3 V1 V6 V7 V8 V4 V2 V8 V5 V2 V8 V6 V3 V7 V7 V3 V6 V8 V4 V5 Graphs - Depth-first Traversing V1 V3 V2 V7 V4 V5 V6 V8

  39. V1 V2 V3 V2 V1 V4 V5 V3 V1 V6 V7 V4 V2 V8 V5 V2 V8 V6 V3 V7 V7 V3 V6 V8 V4 V5 Graphs - Depth-first Traversing V5 V8 V7 V4 V6 V2 V3 V1

  40. 深度优先搜索的递归算法描述 • 1 visited(v0)←1,输出'v0' • 2 p←adjlist(v0) • 3 循环 当 p≠NIL 时,执行 • 4 若 visited (vertex(p))=0 • 5 则 调用 深度优先搜索 (adjlist,vertex(p)) • 6 p←next(p) • {算法结束}

  41. Graphs - Depth-first Traversing Coding DFS (Graph G, int v) { Visited[v]=True; for (w=FirstAdjVex(G,v);w;w=NextAdjVex(G,v,w)) if (!visited[w]) DFS(G,w); } Boolean visited[Max]; DFSTraverse (Graph G) { for (v=0; v<G.vexnum; ++v) visited[v]=False; for (v=0; v<G.vexnum; ++v) if (!visited[v]) DFS(G,v); }

  42. Graph - Breadth-first Traversal广度优先搜索 • 无向图的宽度优先搜索过程是:从图G中某以顶点v0出发,首先依次访问与v0邻接的全部顶点w1, w2,...,wt。然后,再顺次访问与w1,w2,...,wt邻接的尚未访问的全部顶点,再从这些被访问过的 顶点出发,逐次访问与它们邻接的尚未访问过的全部顶点。依次类推,直到所有的顶点全部访问完为止。 • 在上述的搜索过程中,若w1在w2之前被访问,则w1的邻接表也将在w2的邻接表之前被访问。因此对于宽度优先搜索的算法,无须记录所走过的路径,但却需要记录与一个顶点相邻接的全部顶点。由于访问完这些顶点后,将按照先被访问的点,就先访问它的邻接表的方式,进行宽度优先搜索,所以我们用一个先进先出的队Q来存放这些顶点。

  43. V1 V3 V2 V7 V4 V5 V1 V6 V2 V3 V2 V1 V4 V5 V3 V1 V6 V7 V8 V4 V2 V8 V5 V2 V8 V2 V3 V4 V5 V6 V7 V6 V3 V7 V7 V3 V6 V8 V4 V5 Graphs - Breadth-first Traversing V1 V3 V2 V8 V7 V4 V5 V6 V8

  44. 广度优先搜索的算法描述{从v0出发宽度优先搜索图graph}广度优先搜索的算法描述{从v0出发宽度优先搜索图graph} • 1 输出 'v0' • 2 visited(v0)←1 • 3 front←0,rear←0 • 4 p←graph(v0) • 5 循环 执行下列语句,直到 p=∧和front=rear 为止 • 6 循环 当p≠∧时,执行 • 7 v←vertex(p) • 8 若 visited(v)=0 • 9 则 [rear←rear+1 • 10 q(rear)←v • 11 输出 'v' • 12 visited(v)←1] • 13 p←next(p) • 14 若 front≠rear • 15 则 [front←fornt+1 • 16 v←q(front) • 17 p←graph(v)]{算法结束}

  45. Graph - Breadth-first Traversal void BFSTraverse( graph g ) { for(v=0;v<G.vexnum;++v) visited[v] =False; InitQueue(Q); for(v=0;v<G.vexnum;++v) { if ( !visited[v] ) EnQueue( Q,v); while (!QueueEmpty(Q)) { Dequeue(Q,u); visited[u]=ture;Visit(u); for (w=FirstAdjVex(G,u);w;w=NextAdjVex(G,u,w)) if (!visited[w]) EnQueue(Q,w); } } } • Breadth-first requires a FIFO queue

More Related