Lecture 12-2: Introduction to Computer Algorithms beyond Search & Sort
Prim's Algorithm 1. Choose an arbitrary starting vertex v j 2. Find the smallest edge e incident with with a vertex in the vertex set whose inclusion in the edge set does not create a cycle. 3. Include this edge in the edge list and its vertices in the vertex list. 4. Repeat Steps 2 and 3 until all vertices are in the vertex list. Given a weighted graph G consisting of a set of vertices V and a set of edges E with weights, where Prepare a vertex set and an edge set to hold elements selected by Prim's Algorithm. 2 C D F E A G B Exercise Prim's Algorithm on this example weighted graph starting with vertex B and again starting at vertex F. Did you get the same spanning tree? If the trees were distinct, did they have the same value?
Kruskal's Algorithm The minimum spanning tree problem can also be solved using Kruskal's Algorithm. In this approach, we simply choose minimum-weight edges from the graph so long as an edge does not create a cycle in the edge set. We stop choosing edges when every vertex is a node for at least one of the edges in the set and the tree is connected. C D F E A G B C D F E A G B C D F E A G B C D F E A G B C D F E A G B C D F E A G B
Single Source Shortest Path Given a weighted graph G find the minimum weight path from a specified vertex v 0 to every other vertex v1v1 v0v0 v5v5 v4v4 v3v3 v2v2 The single source shortest path problem is as follows. We are given a directed graph with nonnegative edge weights G = (V,E) and a distinguished source vertex,. The problem is to determine the distance from the source vertex to every vertex in the graph.
v1 v2 v3 v4 v5 node minimum list path v1 v2 v3 v4 v v1 v2 v3 v4 v {2} v1 v2 v3 v4 v {2} {24} v1 v2 v3 v4 v {2} {24} {241} 3 5 v1 v2 v3 v4 v {2} {24} {241} 3 5 {2413} 4 v1 v2 v3 v4 v {2} {24} {241} 3 5 {2413} v1v1 v0v0 v5v5 v4v4 v3v3 v2v2 Dijkstra's Algorithm for SSSP
Floyd's Algorithm for Shortest Paths V1V1 V2V2 V5V5 V4V4 V3V – – procedure floyd(W,D:matype) is begin D:=W; for k in 1..n loop for i in 1..n loop for j in 1..n loop D(i,j):=min(D(i,j),D(i,k)+D(k,j)); end loop; end floyd; Floyd's algorithm is very simple to implement. The fact that it works at all is not obvious. Be sure to work through the proof of algorithm correctness in the text.
Bipartite Matching The pairwise matching of members of a bipartite graph is another type of matching. You are searching for a maximal matching (i.e. max number of pairings).
The Augmenting Path Algorithm Given a bipartite graph G n,m we are to find a maximal matching (max number of pairs) between the n nodes of group I and the m nodes of group II. There is a greedy algorithm for the maximal matching problem:
Start with a bipartite graph. Choose arbitrary pairings until no additional matches are possible. In this example nodes C, R and S are not matched. Matching edges are shown in bold Augmenting Path: An Example
We will now build an augmenting path starting from node S. S-A=P-C. We exchange the matched and unmatched edges in this augmenting path increasing the total number of matches by one. A is matched to S B is matched to Q C is matched to P This is a maximal matching because there are no more unmatched nodes in one of the two groups.
Maximal Matching in a General Graph
For General Graphs The Augmenting Path Algorithm does not necessarily produce a maximal matching in a general graph. In an odd cycle there will be two unmatched edges that share a vertex. The our DFS is started from such a vertex and the cycle is traversed the wrong way we will miss the augmenting path.