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...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Sec. 11.7 Exercises 419class KruskalElem implements Comparable {private int v, w, weight;public KruskalElem(int inweight, int inv, int inw){ weight = inweight; v = inv; w = inw; }public int v1() { return v; }public int v2() { return w; }public int key() { return weight; }public int compareTo(KruskalElem that) {if (weight < that.key()) return -1;else if (weight == that.key()) return 0;else return 1;}}static void Kruskal(Graph G) { // Kruskal’s MST algorithmParPtrTree A = new ParPtrTree(G.n()); // Equivalence arrayKruskalElem[] E = new KruskalElem[G.e()]; // Minheap arrayint edgecnt = 0; // Count of edgesfor (int i=0; i1; i++) { // Combine equiv classesKruskalElem temp = H.removemin(); // Next cheapestint v = temp.v1(); int u = temp.v2();if (A.differ(v, u)) { // If in different classesA.UNION(v, u);// Combine equiv classesAddEdge<strong>to</strong>MST(v, u); // Add this edge <strong>to</strong> MSTnumMST--;// One less MST}}}Figure 11.24 An implementation for Kruskal’s algorithm.11.5 Wright a pseudocode algorithm <strong>to</strong> create a DFS tree for an undirected, connectedgraph starting at a specified vertex V.11.6 Show the BFS tree for the graph of Figure 11.25, starting at Vertex 1.11.7 Wright a pseudocode algorithm <strong>to</strong> create a BFS tree for an undirected, connectedgraph starting at a specified vertex V.11.8 The BFS <strong>to</strong>pological sort algorithm can report the existence of a cycle if oneis encountered. Modify this algorithm <strong>to</strong> print the vertices possibly appearingin cycles (if there are any cycles).

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

Saved successfully!

Ooh no, something went wrong!