3.數(shù)據(jù)結(jié)構(gòu)課件cs01版_第1頁
3.數(shù)據(jù)結(jié)構(gòu)課件cs01版_第2頁
3.數(shù)據(jù)結(jié)構(gòu)課件cs01版_第3頁
3.數(shù)據(jù)結(jié)構(gòu)課件cs01版_第4頁
3.數(shù)據(jù)結(jié)構(gòu)課件cs01版_第5頁
免費預覽已結(jié)束,剩余9頁可下載查看

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

1、2 Elementary graph Operations3. Connected Components(1) Is the graph connected? Call dfs(0) or bfs(0) O( e ) Check if there is any vertices missing O( n )Tp = O( n + e )(2) List the connected components of Gvoid connected ( void ) /* determine the connected components of a graph */ int i ; for ( i =

2、 0; i n; i+ ) if ( !visited i ) dfs ( i ) ; /* bfs can be used with no change in Tp */ printf ( “n” ); /* end if */O( n )O( ei1 + ei2 + . ) = O( e )Tp = O( n + e )2 Elementary graph Operations4. Spanning Tree a tree which consists of V( G ) and a subset of E( G )Example A complete graph and three of

3、 its spanning trees【Remarks】 E( G ) = T + N where T is for tree edges and N for non-tree edges. T consists of the edges traversed by the search program. We can add a piece of code to collect these edges into a linked list T.2 Elementary graph Operations【Remarks】 dfs and bfs give different spanning t

4、rees.02130213 Adding an edge in N to a spanning tree, we obtain a cycle. If we introduce the edges in N into the spanning tree one at a time, then we obtain a cycle basis that is, all the cycles are independent, and all other cycles in G can be constructed as a linear combination of the cycles in th

5、e basis. A spanning tree is a minimal subgraph. That is, G G such that V( G ) = V( G ), G is connected, and E( G ) is minimal. G is connected e n 1, and G is connected and e = n 1 G is a tree. e = n 1 for a spanning tree.2 Elementary graph Operations5. Biconnected Components and Articulation PointsA

6、rticulationpointBiconnectedgraph v is an articulation point if G = DeleteVertex( G, v ) has at least 2 connected components. G is a biconnected graph if G is connected and has no articulation points. A biconnected component is a maximal biconnected subgraph.Example0123487569Connected graph0112343587

7、75679Biconnected componentsNote: No edges can be shared by two or more biconnected components. Hence E(G) is partitioned by the biconnected components of G.2 Elementary graph OperationsFinding the biconnected components of a connected undirected G0123487569 Use depth first search to obtain a spannin

8、g tree of Gdfs ( 3 )0123456789Depth first spanning tree34521067980123456789Depth first number (dfn)Note: If u is an ancestor of v, then dfn( u ) dfn( v ). Find the articulation points in G The root is an articulation point iff it has at least 2 children Any other vertex u is an articulation point if

9、f u has at least 1 child, and it is impossible to move down at least 1 step and then jump up to us ancestor.Back edges:= (u, v) in Nand u (or v) isan ancestor ofv (or u).2 Elementary graph Operations34521067980123456789vertexdfnlow012345678943201567980054005598Therefore, u is an articulation point i

10、ff(1) u is the root and has at least 2 children; or(2) u is not the root, and has at least 1 child such that low( child ) dfn( u ).#define MIN2 ( x, y ) ( (x) (y) ? (x) : (y) )short int dfn MAX_VERTICES ;short int low MAX_VERTICES ;int num ;void init ( void ) int i ; for ( i = 0; i next ) w = ptr-ve

11、rtex ; if ( dfn w next ) w = ptr-vertex ; if ( v != w & dfn w dfn u ) /* w is us ancestor or 1 */ push( &top, u, w ); /* push edge to stack */ if ( dfn w = dfn u ) /* u is the last vertex of a biconnected component */ printf( “New biconnected component: “ ) ; do /* pop edge from stack */ pop( &top,

12、&x, &y ) ; printf( “ ”, x, y ) ; while ( !( (x = u) & (y = w) ) ) ; printf( “n” ) ; /* end if */ /* end if ( dfnw 0 for e E( G ). The length of a path P from source to destination is 1. Single Source All DestinationsGiven a source v0. Determine a shortest path to each v V(G) v0 .Note: A path with mi

13、nimal number of vertices is NOT necessarily the shortest.v0v1v2v3v4v52010501515201035303453) v0 v2 v3 v12) v0 v2 v34) v0 v41) v0 v2PathLength10254545non-decreasing4 Shortest Paths & Transitive ClosureLet S = v0 and vis whose shortest paths have been found For any u S, define distance u = minimal len

14、gth of path v0 ( vi S ) u . If the paths are generated in non-decreasing order, then the shortest path must go through ONLY vi S ; Why? If it is not true, thenthere must be a vertex w on this paththat is not in S. Then . u is chosen so that distance u = min wS | distance w (If u is not unique, then

15、we may select any of them) ; if distance u1 distance u2 and we add u1 into S, then distance u2 may change. If so, a shorter path from v0 to u2 must go through u1 and distance u2 = distance u1 + length().Implementation vi = 0, 1, ., n1 Note: LARGE_NO must be greater than maxw(ei), and maxdistancei +L

16、ARGE_NO wont overflow.4 Shortest Paths & Transitive Closurevoid shortestpath ( int v, int cost MAX_VERTICES , int distance , int n, short int found ) int i, u, w ; for ( i = 0; i n; i+ ) /* initialization */ found i = FALSE ; distance i = cost v i ; /* end for-loop */ found v = TRUE ; distance v = 0 ; for ( i = 0; i n2; i+ ) u = choose ( distance, n, found ) ; /* O( n ) */ /* find the min distance not yet

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論