tree type graphs ? ? ? ?. all graph representations plus adapted representations. son-brother...

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

If you can't read please download the document

Upload: arnold-simon

Post on 19-Jan-2018

224 views

Category:

Documents


0 download

TRANSCRIPT

Tree type graphs ? ? ? ? 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) Apply a rule for systematic visitation of tree nodes Adaptations of graph traversing: Breadth (BF) Level traversing Depth (DF) A-preorder A-postorder 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 Pe niveluri (reprezentare cu structuri dinamice) parcurgere_pe_niveluri( R ) { Coad C = NULL; adaug ( C, R ); ct 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 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 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 Parcurgeri n adncime (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 ); } } i j arc asc. : weight total ( ) (6,7) ( ) (2,5) ( ) (2,7) ( ) (4,5) ( ) (1,2) ( ) (1,4) ( ) (3,5) ( ) Current step (edge nr.) Nr. of edges added 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 < ( ) 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