Presentation is loading. Please wait.

Presentation is loading. Please wait.

Tree type graphs ? ? ? ?.  All graph representations plus adapted representations.  Son-Brother representation  N: number of vertices.

Similar presentations


Presentation on theme: "Tree type graphs ? ? ? ?.  All graph representations plus adapted representations.  Son-Brother representation  N: number of vertices."— Presentation transcript:

1 Tree type graphs ? ? ? ?

2

3

4

5

6

7

8

9  All graph representations plus adapted representations.  Son-Brother representation  N: number of vertices (nodes)  R: root node  Son(i): identifier of the first descendant of node i  Brother(i): identifier of the first descendant of the parent of i that follows right after i  Inf(i): information attached to node i  Missing value: conventional value (0, -1…)

10

11

12  Apply a rule for systematic visitation of tree nodes  Adaptations of graph traversing:  Breadth (BF) ▪Level traversing  Depth (DF) ▪A-preorder ▪A-postorder

13  Level traversing (Son-Brother representation) level_traversing( R, SON[], BROTHER[], n ) { Queue C = NULL; add( C, R ); while ( C ) { extract( C, v ); process( v ); v = SON[v]; while ( v ) { add( C, v); v = BROTHER[v]; } } } 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16

14  Pe niveluri (reprezentare cu structuri dinamice) parcurgere_pe_niveluri( R ) { Coadă C = NULL; adaugă ( C, R ); cît timp ( C ) { extrage( C, v ); Prelucrează( v ); pentru ( i=0,n-1 ) dacă ( v->fiu[i]) adaugă( C, v->fiu[i]); } } 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16

15  A-preorder (Son-Brother representation)  Visit (and process) current node (root) and identify its descendants. Apply the same rule for all the subtrees that have as root one of the descendants of current node. A_preorder ( R ) { if( R ) { process( R ); A_preorder( SON[R] ); A_preorder( BROTHER[R] ); } } 1, 2, 5, 6, 9, 10, 11, 12, 13, 7, 3, 4, 8, 14, 15, 16

16  A-postorder (Son-Brother representation)  Identify the descendants of current node (root) and visit each of the subtrees that have as root one of the descendants of current node, then visit (process) current node (root). For each subtree the same rule applies. A_postorder ( R ) { if( R ) { A_postorder( SON[R] ); process( R ); A_postorder( BROTHER[R] ); } } 5, 9, 10, 11, 12, 13, 6, 7, 2, 3, 14, 15, 16, 8, 4, 1

17  Parcurgeri în adîncime (reprezentare cu structuri dinamice) A_preordine ( R ) { dacă( R ) { Prelucrează( R ); pentru( i=0,n-1) A_preordine( R->fiu[i] ); } } A_postordine ( R ) { dacă(R) { pentru( i=0,n-1 ) A_postordine( R->fiu[i] ); Prelucrează( R ); } }

18

19

20

21 i j arc asc. : 1 2 3 4 5 6 7 weight total ( -1 -1 -1 -1 -1 -1 -1 ) 0 0 1 (6,7) ( -1 -1 -1 -1 -1 7 -2 ) 1 1 1 2 (2,5) ( -1 5 -1 -1 -2 7 -2 ) 2 3 2 3 (2,7) ( -1 5 -1 -1 7 7 -4 ) 2 5 3 4 (4,5) ( -1 5 -1 7 7 7 -5 ) 2 7 4 5 (1,2) ( 7 5 -1 7 7 7 -6 ) 3 10 5 5 (1,4) ( 7 7 -1 7 7 7 -6 ) 10 6 6 (3,5) ( 7 7 7 7 7 7 -7 ) 3 13 6 7 1 2 5 2 2 7 2 4 5 2 1 2 3 1 4 3 3 5 3 3 7 3 3 4 4 4 6 5 5 6 6 Current step (edge nr.) Nr. of edges added

22  Function for finding the root (asc – ascendant) int root( int v, int asc[]) { int u; u = v; while( asc[u] >= 0 ) u = asc[u]; return u; } Ex.: v = 2 u = 2 asc[2]=5 u = 5 asc[5]=7 u = 7 asc[7]=-4 < 0 1 2 3 4 5 6 7 ( -1 5 -1 -1 7 7 -4 )

23 int kruskal(int a[][3],int nm, int nv, int b[][3]) { int asc[50], i, j, v1, v2, r1, r2; int c=0; for(i=0; i<nv; i++) asc[i]=-1; for(i=j=0; j<nv-1; i++) { v1=a[i][0]; v2=a[i][1]; r1=root(v1,asc); r2=root(v2,asc); if( r1 != r2 ) { if( asc[r1] < asc[r2] ) { asc[r1]+=asc[r2]; asc[r2]=r1; } else { asc[r2]+=asc[r1]; asc[r1]=r2; } b[j][0]=a[i][0]; b[j][1]=a[i][1]; b[j][2]=a[i][2]; c+=a[i][2]; j++; } } return c; }

24

25  Homework  Implement Prim’s algorithm  Prim or Kruskal?

26  Homework  Study all the examples in the problem book, Graphs chapter: ▪ Edition 2015: chapter 3 ▪ Edition 2012: chapter 6

27 Spor la învăat!


Download ppt "Tree type graphs ? ? ? ?.  All graph representations plus adapted representations.  Son-Brother representation  N: number of vertices."

Similar presentations


Ads by Google