lecture 12-2: introduction to computer algorithms beyond search & sort

12
Lecture 12-2: Introduction to Computer Algorithms beyond Search & Sort

Upload: anthony-bruce

Post on 27-Dec-2015

213 views

Category:

Documents


0 download

TRANSCRIPT

Lecture 12-2:

Introduction to Computer Algorithmsbeyond Search & Sort

Prim's Algorithm

1. Choose an arbitrary starting vertex vj

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.

VvvandEvve jiji ,),(

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?

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

Single Source Shortest Path

Given a weighted graph G find the minimum weight path from a specified vertex v0 to every other vertex.

2

11

11 3

4

5

3

6

v1

v0

v5

v4

v3

v2

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.

Vs

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

11

11 3

4

5

3

6

v1

v0

v5

v4

v3

v2

Dijkstra's Algorithm for SSSP

Floyd's Algorithm for Shortest Paths

V1

V2

V5

V4V3

1

5

1

1

26

1

3

2

1 2 3 4 51 0 2 – 2 -2 3 0 6 - -3 - - 0 - 14 5 – 1 0 15 1 - - - 0

procedure floyd(W,D:matype) isbegin 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 loop; 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 Gn,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.