Download presentation
Presentation is loading. Please wait.
Published byAnthony Bruce Modified over 9 years ago
1
Lecture 12-2: Introduction to Computer Algorithms beyond Search & Sort
2
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 4 2 3 5 1 2 1 2 1 2 1 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?
3
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 4 2 3 5 1 2 1 2 2 1 2 1 C D F E A G B 4 2 3 5 1 2 1 2 2 1 2 1 C D F E A G B 4 2 3 5 1 2 1 2 2 1 2 1 C D F E A G B 4 2 3 5 1 2 1 2 2 1 2 1 C D F E A G B 4 2 3 5 1 2 1 2 2 1 2 1 C D F E A G B 4 2 3 5 1 2 1 2 2 1 2 1
4
Single Source Shortest Path Given a weighted graph G find the minimum weight path from a specified vertex v 0 to every other vertex. 2 1 1 1 13 4 5 3 6 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.
5
v1 v2 v3 v4 v5 node minimum list path v1 v2 v3 v4 v5 5 1 4 - 6 v1 v2 v3 v4 v5 5 1 4 - 6 {2} 3 4 2 6 v1 v2 v3 v4 v5 5 1 4 - 6 {2} 3 4 2 6 {24} 3 3 5 v1 v2 v3 v4 v5 5 1 4 - 6 {2} 3 4 2 6 {24} 3 3 5 {241} 3 5 v1 v2 v3 v4 v5 5 1 4 - 6 {2} 3 4 2 6 {24} 3 3 5 {241} 3 5 {2413} 4 v1 v2 v3 v4 v5 5 1 4 - 6 {2} 3 4 2 6 {24} 3 3 5 {241} 3 5 {2413} 4 2 1 1 1 13 4 5 3 6 v1v1 v0v0 v5v5 v4v4 v3v3 v2v2 Dijkstra's Algorithm for SSSP
6
Floyd's Algorithm for Shortest Paths V1V1 V2V2 V5V5 V4V4 V3V3 1 5 1 1 2 6 1 3 2 1 2 3 4 5 1 0 2 – 2 - 2 3 0 6 - - 3 - - 0 - 1 4 5 – 1 0 1 5 1 - - - 0 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.
7
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).
8
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:
9
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
10
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.
11
Maximal Matching in a General Graph
12
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.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.