• No results found

B AB 5 C AC 3 D ABGED 9 E ABGE 7 F ABGEF 8 G ABG 6 A BEDA 3 C BC 1 D BCD 2 E BE 1 F BEF 2 G BG 1

N/A
N/A
Protected

Academic year: 2021

Share "B AB 5 C AC 3 D ABGED 9 E ABGE 7 F ABGEF 8 G ABG 6 A BEDA 3 C BC 1 D BCD 2 E BE 1 F BEF 2 G BG 1"

Copied!
6
0
0

Loading.... (view fulltext now)

Full text

(1)

p.339 9.5

a. Find the shortest path from A

to all other vertices for the

graph in Figure 9.80.

b. Find the shortest unweighted

path from B to all other

vertices for the graph in

Figure 9.80.

A B C D E F G 1 1 1 5 2 3 3 7 2 7 2 6

a.

source destination path cost

B AB 5 C AC 3 D ABGED 9 E ABGE 7 F ABGEF 8 A G ABG 6

b.

Source Destination path cost

A BEDA 3 C BC 1 D BCD 2 E BE 1 F BEF 2 B G BG 1

p.340 9.10

(

ZOJ 2526

)

a.

Explain how to modify Dijkstra's algorithm to produce a

count of the number of different minimum paths from v to

w.

b.

Explain how to modify Dijkstra's algorithm so that if there

is more than one minimum path from v to w, a path with the

fewest number of edges is chosen.

(2)

Answer a:

void Dijkstra( Table T )

{ /* T[ ].Count is initialized to be 0. T[start].Count = 1 */

vertex v, w; for ( ; ; ) {

v = smallest unknown distance vertex; if ( v == NotAVertex )

break;

T[v].Known = True;

for ( each w adjacent to v ) if( !T[w].Known )

if( T[v].Dist + Cvw < T[w].Dist ) {

Decrease( T[w].Dist to T[v]+Cvw ) T[w].Path = v;

T[w].Count = T[v].Count; /* NOT T[w].Count = 1 */

}

else if( T[v].Dist + Cvw == T[w].Dist )

T[w].Count += T[v].Count; /* NOT T[w].Count += 1 */

} }

Answer b:

void Dijkstra( Table T )

{ /* T[ ].Count is initialized to be 0 */

vertex v, w; for ( ; ; ) {

v = smallest unknown distance vertex; if ( v == NotAVertex )

break;

T[v].Known = True;

for ( each w adjacent to v ) if( !T[w].Known ) if( T[v].Dist + Cvw < T[w].Dist ) { Decrease( T[w].Dist to T[v]+Cvw ) T[w].Path = v; T[w].Count = T[v].Count + 1; }

else if( ( T[v].Dist + Cvw == T[w].Dist )

&& ( T[v].Count + 1 < T[w].Count ) ) { T[w].Count = T[v].Count + 1;

T[w].Path = v; /* DO NOT forget this */

}

} }

(3)

p.341 9.11

Find the maximum flow in the network of Figure 9.79.

Answer:

A B C

s D E F

p.341 9.15

a.

Find a minimum spanning tree for the graph in Figure 9.82

using both Prim's and Kruskal's algorithms.

b.

Is this minimum spanning tree unique? Why?

Answer a:

They are the same

Answer b:

This minimum spanning tree is not unique. For example, another minimum spanning tree obtained from Prim’s algorithm is:

A B C D E F G H I J 4 3 2 2 7 2 1 1 3 A B C D E F G H I J 4 3 2 2 7 2 1 1 3 G H I 1 2 2 4 4 3 3 3 3 2 2 6 4 4 2 0 0 0 0 0 t 4

(4)

p.339 9.27

Write a program to find the strongly connected components in

a digraph.

#define MaxVertices 100 /* maximum number of vertices */

typedef int Vertex; /* vertices are numbered from 0 to MaxVertices1 */

typedef enum {FALSE, TRUE} boolean;

/* declarations for a graph with adjacency list representation */

#ifndef _Graph_h struct VNode;

typedef struct VNode *PtrToVNode; struct GNode;

typedef struct GNode *PtrToGNode; typedef PtrToGNode Graph;

/* create a graph with NumOfVertices vertices and no edge */

Graph CreateGraph( int NumOfVertices );

/* insert edge V->W to G */

boolean InsertEdge( Vertex V, Vertex W, Graph G );

/* reverse all the edges in G and return the resulting graph Gr */

Graph ReverseGraph( Graph G );

/* free spaces taken by G */

void DeleteGraph( Graph G );

/* implementations of the above 4 functions are omitted */

#endif /*_Graph_h */ struct VNode { Vertex Vert; PtrToVNode Next; }; struct GNode { int NumOfVertices; int NumOfEdges; PtrToVNode *Array; };

boolean Visited[MaxVertices]; /* global mark for visited vertices */

Vertex DfsOrder[MaxVertices]; /* store vertices in dfs order */

(5)

void PostOrder( Vertex V ) {

/* store vertices during postorder dfs */

DfsOrder[DfsNum++] = V; }

void PrintV( Vertex V ) {

/* print V during postorder dfs */

printf("%d ", V); }

void PostorderDfs( Vertex V, Graph G, void (*f)(Vertex V) ) {

/* postorder dfs template with visiting function f */

PtrToVNode W; Visited[V] = TRUE;

for ( W=G->Array[V]->Next; W; W=W->Next ) if ( !Visited[W->Vert] )

PostorderDfs( W->Vert, G, (*f) ); (*f)(V);

}

void StronglyConnectedComponents( Graph G ) {

/* print the strongly connected components in G */ /* output format: { V11, V12, ... } { V21, V22, ... } ... ...*/ Graph Gr; Vertex V;

/* Step 1: mark the postorder dfs number for each vertex in G */

InitializeVisited( G->NumOfVertices ); /* Visited[ ] is initialized to be FALSE */

DfsNum = 0;

for ( V=0; V<G->NumOfVertices; V++ ) { if ( !Visited[V] )

PostorderDfs( V, G, PostOrder ); } /* end for */

/* Step 2: reverse edges in G and save the resulting graph in Gr */

Gr = ReverseGraph( G ); if ( !Gr )

(6)

printf("Program failed: cannot reverse graph.\n"); else {

/* Step 3: print components by postorder dfs on Gr */

InitializeVisited( Gr->NumOfVertices ); while ( DfsNum −− ) {

/* always start at the vertex with the largest dfs number */

V = DfsOrder[DfsNum]; if ( !Visited[V] ) {

/* print this component in a line*/

printf( "{ " );

PostorderDfs( V, Gr, PrintV ); printf( "}\n" );

} /* end - if */

} /* end - while */

DeleteGraph( Gr ); /* free space */

} /* end - else */

}

Sketch of the proof of correctness:

1. V, W Comp(G) ⇒∃ Path(V->W) and Path(W->V) in both G and Gr; 2. V, W Comp(G) V, W DfsT(G) and DfsT(Gr);

3. For V DfsT(Gr) with X as the root, Path(X->V) in Gr, and hence Path(V->X) in G. 4. Step 3 V, X DfsT(G);

5. DfsNum(X) > DfsNum(V) V must be numbered before X in DfsT(G); 6. Step 5 V is a descendant of X in DfsT(G) since it was a postorder visit; 7. Step 6 ⇒∃ Path(X->V) in G. 1 2 3 4 1 2 3 0 1 2 DfsT(G) 1 3 2 1 0 2 DfsT(Gr) G/Gr

References

Related documents

Note that the financial aid administrator at your school may require you to provide proof that your parent is a dislocated worker, if you answered “Yes” to question 85, or that you

Toward Operational Compensation of Ionospheric Effects in SAR Interferograms: The Split-Spectrum Method Giorgio Gomba, Alessandro Parizzi, Francesco De Zan, Michael Eineder,

Myoblast function is rate limiting for muscle growth ( Pavlath et  al., 1989 ; Allen et  al., 1999 ), and myoblasts isolated from IUGR fetal sheep exhibit intrinsically reduced

Make measurements on timeslot 0 [FREQUENCY] {Timeslot Off} [Enter] Activate the ORFS measurement (figure 25) [MEASURE] {GMSK Output RF The default setting measures spectrum

A este respecto, lo primero que cabe señalar es que la noción de contradicción de la que habla Deleuze difiere de la noción de contradicción que emplea Hegel cuando define así

A mixture of fine and coarse spherical particles was observed over Bucharest, likely re- lated to the presence of smoke from European fires, whereas at Athens mainly fine particles

Some qualifying countries have also experienced strong growth in foreign direct investment aimed at taking advantage of AGOA with positive spin-offs for increased employment

U nastavku rada opisani su: komunikacijsko-funkcionalan pristup učenju, koji je usko povezan s napuštanjem tradicionalnog poučavanja te usvajanjem metoda i postupaka koje će