12.07.2015 Views

A Practical Introduction to Data Structures and Algorithm Analysis

A Practical Introduction to Data Structures and Algorithm Analysis

A Practical Introduction to Data Structures and Algorithm Analysis

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Sec. 11.8 Projects 421MST, show the result on the equivalence array, (e.g., show the array as inFigure 6.7).11.19 Write an algorithm <strong>to</strong> find a maximum cost spanning tree, that is, the spanningtree with highest possible cost.11.20 When can Prim’s <strong>and</strong> Kruskal’s algorithms yield different MSTs?11.21 Prove that, if the costs for the edges of Graph G are distinct, then only oneMST exists for G.11.22 Does either Prim’s or Kruskal’s algorithm work if there are negative edgeweights?11.23 Consider the collection of edges selected by Dijkstra’s algorithm as the shortestpaths <strong>to</strong> the graph’s vertices from the start vertex. Do these edges forma spanning tree (not necessarily of minimum cost)? Do these edges form anMST? Explain why or why not.11.24 Prove that a tree is a bipartite graph.11.25 Prove that any tree (i.e., a connected, undirected graph with no cycles) canbe two-colored. (A graph can be two colored if every vertex can be assignedone of two colors such that no adjacent vertices have the same color.)11.26 Write an algorithm that determines if an arbitrary undirected graph is a bipartitegraph. If the graph is bipartite, then your algorithm should also identifythe vertices as <strong>to</strong> which of the two partitions each belongs <strong>to</strong>.11.8 Projects11.1 Design a format for s<strong>to</strong>ring graphs in files. Then implement two functions:one <strong>to</strong> read a graph from a file <strong>and</strong> the other <strong>to</strong> write a graph <strong>to</strong> a file. Testyour functions by implementing a complete MST program that reads an undirectedgraph in from a file, constructs the MST, <strong>and</strong> then writes <strong>to</strong> a secondfile the graph representing the MST.11.2 An undirected graph need not explicitly s<strong>to</strong>re two separate directed edges <strong>to</strong>represent a single undirected edge. An alternative would be <strong>to</strong> s<strong>to</strong>re only asingle undirected edge (I, J) <strong>to</strong> connect Vertices I <strong>and</strong> J. However, what if theuser asks for edge (J, I)? We can solve this problem by consistently s<strong>to</strong>ringthe edge such that the lesser of I <strong>and</strong> J always comes first. Thus, if we havean edge connecting Vertices 5 <strong>and</strong> 3, requests for edge (5, 3) <strong>and</strong> (3, 5) bothmap <strong>to</strong> (3, 5) because 3 < 5.Looking at the adjacency matrix, we notice that only the lower triangle ofthe array is used. Thus we could cut the space required by the adjacencymatrix from |V| 2 positions <strong>to</strong> |V|(|V|−1)/2 positions. Read Section 12.2 on

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!