Cs 332 algorithms
Download
1 / 63

CS 332: Algorithms - PowerPoint PPT Presentation


  • 232 Views
  • Uploaded on

CS 332: Algorithms. Topological Sort Minimum Spanning Trees. Review: Breadth-First Search. BFS(G, s) { initialize vertices; Q = {s}; // Q is a queue (duh); initialize to s while (Q not empty) { u = RemoveTop(Q); for each v  u->adj {

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'CS 332: Algorithms' - andrew


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
Cs 332 algorithms l.jpg

CS 332: Algorithms

Topological Sort

Minimum Spanning Trees

David Luebke 13/10/2014


Review breadth first search l.jpg
Review: Breadth-First Search

BFS(G, s) {

initialize vertices;

Q = {s}; // Q is a queue (duh); initialize to s

while (Q not empty) {

u = RemoveTop(Q);

for each v  u->adj {

if (v->color == WHITE)

v->color = GREY;

v->d = u->d + 1;

v->p = u;

Enqueue(Q, v);

}

u->color = BLACK;

}

}

v->d represents depth in tree

v->p represents parent in tree

David Luebke 23/10/2014


Review dfs code l.jpg

DFS(G)

{

for each vertex u  G->V

{

u->color = WHITE;

}

time = 0;

for each vertex u  G->V

{

if (u->color == WHITE)

DFS_Visit(u);

}

}

DFS_Visit(u)

{

u->color = YELLOW;

time = time+1;

u->d = time;

for each v  u->Adj[]

{

if (v->color == WHITE)

DFS_Visit(v);

}

u->color = BLACK;

time = time+1;

u->f = time;

}

Review: DFS Code

David Luebke 33/10/2014


Review dfs example l.jpg
Review: DFS Example

sourcevertex

David Luebke 43/10/2014


Review dfs example5 l.jpg
Review: DFS Example

sourcevertex

d f

1 |

|

|

|

|

|

|

|

David Luebke 53/10/2014


Review dfs example6 l.jpg
Review: DFS Example

sourcevertex

d f

1 |

|

|

2 |

|

|

|

|

David Luebke 63/10/2014


Review dfs example7 l.jpg
Review: DFS Example

sourcevertex

d f

1 |

|

|

2 |

|

3 |

|

|

David Luebke 73/10/2014


Review dfs example8 l.jpg
Review: DFS Example

sourcevertex

d f

1 |

|

|

2 |

|

3 | 4

|

|

David Luebke 83/10/2014


Review dfs example9 l.jpg
Review: DFS Example

sourcevertex

d f

1 |

|

|

2 |

|

3 | 4

5 |

|

David Luebke 93/10/2014


Review dfs example10 l.jpg
Review: DFS Example

sourcevertex

d f

1 |

|

|

2 |

|

3 | 4

5 | 6

|

David Luebke 103/10/2014


Review dfs example11 l.jpg
Review: DFS Example

sourcevertex

d f

1 |

8 |

|

2 | 7

|

3 | 4

5 | 6

|

David Luebke 113/10/2014


Review dfs example12 l.jpg
Review: DFS Example

sourcevertex

d f

1 |

8 |

|

2 | 7

|

3 | 4

5 | 6

|

David Luebke 123/10/2014


Review dfs example13 l.jpg
Review: DFS Example

sourcevertex

d f

1 |

8 |

|

2 | 7

9 |

3 | 4

5 | 6

|

David Luebke 133/10/2014


Review dfs example14 l.jpg
Review: DFS Example

sourcevertex

d f

1 |

8 |

|

2 | 7

9 |10

3 | 4

5 | 6

|

David Luebke 143/10/2014


Review dfs example15 l.jpg
Review: DFS Example

sourcevertex

d f

1 |

8 |11

|

2 | 7

9 |10

3 | 4

5 | 6

|

David Luebke 153/10/2014


Review dfs example16 l.jpg
Review: DFS Example

sourcevertex

d f

1 |12

8 |11

|

2 | 7

9 |10

3 | 4

5 | 6

|

David Luebke 163/10/2014


Review dfs example17 l.jpg
Review: DFS Example

sourcevertex

d f

1 |12

8 |11

13|

2 | 7

9 |10

3 | 4

5 | 6

|

David Luebke 173/10/2014


Review dfs example18 l.jpg
Review: DFS Example

sourcevertex

d f

1 |12

8 |11

13|

2 | 7

9 |10

3 | 4

5 | 6

14|

David Luebke 183/10/2014


Review dfs example19 l.jpg
Review: DFS Example

sourcevertex

d f

1 |12

8 |11

13|

2 | 7

9 |10

3 | 4

5 | 6

14|15

David Luebke 193/10/2014


Review dfs example20 l.jpg
Review: DFS Example

sourcevertex

d f

1 |12

8 |11

13|16

2 | 7

9 |10

3 | 4

5 | 6

14|15

David Luebke 203/10/2014


Review kinds of edges l.jpg
Review: Kinds Of Edges

  • Thm: If G is undirected, a DFS produces only tree and back edges

  • Thm: An undirected graph is acyclic iff a DFS yields no back edges

  • Thus, can run DFS to find cycles

David Luebke 213/10/2014


Review kinds of edges22 l.jpg
Review: Kinds of Edges

sourcevertex

d f

1 |12

8 |11

13|16

2 | 7

9 |10

3 | 4

5 | 6

14|15

Tree edges

Back edges

Forward edges

Cross edges

David Luebke 223/10/2014


Dfs and cycles l.jpg
DFS And Cycles

  • Running time: O(V+E)

  • We can actually determine if cycles exist in O(V) time:

    • In an undirected acyclic forest, |E|  |V| - 1

    • So count the edges: if ever see |V| distinct edges, must have seen a back edge along the way

    • Why not just test if |E| <|V| and answer the question in constant time?

David Luebke 233/10/2014


Directed acyclic graphs l.jpg
Directed Acyclic Graphs

  • A directed acyclic graph or DAG is a directed graph with no directed cycles:

David Luebke 243/10/2014


Dfs and dags l.jpg
DFS and DAGs

  • Argue that a directed graph G is acyclic iff a DFS of G yields no back edges:

    • Forward: if G is acyclic, will be no back edges

      • Trivial: a back edge implies a cycle

    • Backward: if no back edges, G is acyclic

      • Argue contrapositive: G has a cycle   a back edge

        • Let v be the vertex on the cycle first discovered, and u be the predecessor of v on the cycle

        • When v discovered, whole cycle is white

        • Must visit everything reachable from v before returning from DFS-Visit()

        • So path from uv is yellowyellow, thus (u, v) is a back edge

David Luebke 253/10/2014


Topological sort l.jpg
Topological Sort

  • Topological sort of a DAG:

    • Linear ordering of all vertices in graph G such that vertex u comes before vertex v if edge (u, v)  G

  • Real-world example: getting dressed

David Luebke 263/10/2014


Getting dressed l.jpg
Getting Dressed

Underwear

Socks

Watch

Pants

Shoes

Shirt

Belt

Tie

Jacket

David Luebke 273/10/2014


Getting dressed28 l.jpg
Getting Dressed

Underwear

Socks

Watch

Pants

Shoes

Shirt

Belt

Tie

Jacket

Socks

Underwear

Pants

Shoes

Watch

Shirt

Belt

Tie

Jacket

David Luebke 283/10/2014


Topological sort algorithm l.jpg
Topological Sort Algorithm

Topological-Sort()

{

Run DFS

When a vertex is finished, output it

Vertices are output in reverse topological order

}

  • Time: O(V+E)

  • Correctness: Want to prove that (u,v)  G  uf > vf

David Luebke 293/10/2014


Correctness of topological sort l.jpg
Correctness of Topological Sort

  • Claim: (u,v)  G  uf > vf

    • When (u,v) is explored, u is yellow

      • v = yellow  (u,v) is back edge. Contradiction (Why?)

      • v = white  v becomes descendent of u  vf < uf (since must finish v before backtracking and finishing u)

      • v = black  v already finished  vf < uf

David Luebke 303/10/2014


Minimum spanning tree l.jpg
Minimum Spanning Tree

  • Problem: given a connected, undirected, weighted graph:

6

4

5

9

14

2

10

15

3

8

David Luebke 313/10/2014


Minimum spanning tree32 l.jpg
Minimum Spanning Tree

  • Problem: given a connected, undirected, weighted graph, find a spanning tree using edges that minimize the total weight

6

4

5

9

14

2

10

15

3

8

David Luebke 323/10/2014


Minimum spanning tree33 l.jpg
Minimum Spanning Tree

  • Which edges form the minimum spanning tree (MST) of the below graph?

A

6

4

5

9

H

B

C

14

2

10

15

G

E

D

3

8

F

David Luebke 333/10/2014


Minimum spanning tree34 l.jpg
Minimum Spanning Tree

  • Answer:

A

6

4

5

9

H

B

C

14

2

10

15

G

E

D

3

8

F

David Luebke 343/10/2014


Minimum spanning tree35 l.jpg
Minimum Spanning Tree

  • MSTs satisfy the optimal substructure property: an optimal tree is composed of optimal subtrees

    • Let T be an MST of G with an edge (u,v) in the middle

    • Removing (u,v) partitions T into two trees T1 and T2

    • Claim: T1 is an MST of G1 = (V1,E1), and T2 is an MST of G2 = (V2,E2) (Do V1 and V2 share vertices? Why?)

    • Proof: w(T) = w(u,v) + w(T1) + w(T2)(There can’t be a better tree than T1 or T2, or T would be suboptimal)

David Luebke 353/10/2014


Minimum spanning tree36 l.jpg
Minimum Spanning Tree

  • Thm:

    • Let T be MST of G, and let A  T be subtree of T

    • Let (u,v) be min-weight edge connecting A to V-A

    • Then (u,v)  T

David Luebke 363/10/2014


Minimum spanning tree37 l.jpg
Minimum Spanning Tree

  • Thm:

    • Let T be MST of G, and let A  T be subtree of T

    • Let (u,v) be min-weight edge connecting A to V-A

    • Then (u,v)  T

  • Proof: in book (see Thm 24.1)

David Luebke 373/10/2014


Prim s algorithm l.jpg
Prim’s Algorithm

MST-Prim(G, w, r)

Q = V[G];

for each u Q

key[u] = ;

key[r] = 0;

p[r] = NULL;

while (Q not empty)

u = ExtractMin(Q);

for each v Adj[u]

if (v Q and w(u,v) < key[v])

p[v] = u;

key[v] = w(u,v);

David Luebke 383/10/2014


Prim s algorithm39 l.jpg
Prim’s Algorithm

MST-Prim(G, w, r)

Q = V[G];

for each u Q

key[u] = ;

key[r] = 0;

p[r] = NULL;

while (Q not empty)

u = ExtractMin(Q);

for each v Adj[u]

if (v Q and w(u,v) < key[v])

p[v] = u;

key[v] = w(u,v);

6

4

9

5

14

2

10

15

3

8

Run on example graph

David Luebke 393/10/2014


Prim s algorithm40 l.jpg
Prim’s Algorithm

MST-Prim(G, w, r)

Q = V[G];

for each u Q

key[u] = ;

key[r] = 0;

p[r] = NULL;

while (Q not empty)

u = ExtractMin(Q);

for each v Adj[u]

if (v Q and w(u,v) < key[v])

p[v] = u;

key[v] = w(u,v);

6

4

9

5

14

2

10

15

3

8

Run on example graph

David Luebke 403/10/2014


Prim s algorithm41 l.jpg
Prim’s Algorithm

MST-Prim(G, w, r)

Q = V[G];

for each u Q

key[u] = ;

key[r] = 0;

p[r] = NULL;

while (Q not empty)

u = ExtractMin(Q);

for each v Adj[u]

if (v Q and w(u,v) < key[v])

p[v] = u;

key[v] = w(u,v);

6

4

9

5

14

2

10

15

r

0

3

8

Pick a start vertex r

David Luebke 413/10/2014


Prim s algorithm42 l.jpg
Prim’s Algorithm

MST-Prim(G, w, r)

Q = V[G];

for each u Q

key[u] = ;

key[r] = 0;

p[r] = NULL;

while (Q not empty)

u = ExtractMin(Q);

for each v Adj[u]

if (v Q and w(u,v) < key[v])

p[v] = u;

key[v] = w(u,v);

6

4

9

5

14

2

10

15

u

0

3

8

Red vertices have been removed from Q

David Luebke 423/10/2014


Prim s algorithm43 l.jpg
Prim’s Algorithm

MST-Prim(G, w, r)

Q = V[G];

for each u Q

key[u] = ;

key[r] = 0;

p[r] = NULL;

while (Q not empty)

u = ExtractMin(Q);

for each v Adj[u]

if (v Q and w(u,v) < key[v])

p[v] = u;

key[v] = w(u,v);

6

4

9

5

14

2

10

15

u

0

3

8

3

Red arrows indicate parent pointers

David Luebke 433/10/2014


Prim s algorithm44 l.jpg
Prim’s Algorithm

MST-Prim(G, w, r)

Q = V[G];

for each u Q

key[u] = ;

key[r] = 0;

p[r] = NULL;

while (Q not empty)

u = ExtractMin(Q);

for each v Adj[u]

if (v Q and w(u,v) < key[v])

p[v] = u;

key[v] = w(u,v);

6

4

9

5

14

14

2

10

15

u

0

3

8

3

David Luebke 443/10/2014


Prim s algorithm45 l.jpg
Prim’s Algorithm

MST-Prim(G, w, r)

Q = V[G];

for each u Q

key[u] = ;

key[r] = 0;

p[r] = NULL;

while (Q not empty)

u = ExtractMin(Q);

for each v Adj[u]

if (v Q and w(u,v) < key[v])

p[v] = u;

key[v] = w(u,v);

6

4

9

5

14

14

2

10

15

0

3

8

3

u

David Luebke 453/10/2014


Prim s algorithm46 l.jpg
Prim’s Algorithm

MST-Prim(G, w, r)

Q = V[G];

for each u Q

key[u] = ;

key[r] = 0;

p[r] = NULL;

while (Q not empty)

u = ExtractMin(Q);

for each v Adj[u]

if (v Q and w(u,v) < key[v])

p[v] = u;

key[v] = w(u,v);

6

4

9

5

14

14

2

10

15

0

8

3

8

3

u

David Luebke 463/10/2014


Prim s algorithm47 l.jpg
Prim’s Algorithm

MST-Prim(G, w, r)

Q = V[G];

for each u Q

key[u] = ;

key[r] = 0;

p[r] = NULL;

while (Q not empty)

u = ExtractMin(Q);

for each v Adj[u]

if (v Q and w(u,v) < key[v])

p[v] = u;

key[v] = w(u,v);

6

4

9

5

10

14

2

10

15

0

8

3

8

3

u

David Luebke 473/10/2014


Prim s algorithm48 l.jpg
Prim’s Algorithm

MST-Prim(G, w, r)

Q = V[G];

for each u Q

key[u] = ;

key[r] = 0;

p[r] = NULL;

while (Q not empty)

u = ExtractMin(Q);

for each v Adj[u]

if (v Q and w(u,v) < key[v])

p[v] = u;

key[v] = w(u,v);

6

4

9

5

10

14

2

10

15

0

8

3

8

3

u

David Luebke 483/10/2014


Prim s algorithm49 l.jpg
Prim’s Algorithm

MST-Prim(G, w, r)

Q = V[G];

for each u Q

key[u] = ;

key[r] = 0;

p[r] = NULL;

while (Q not empty)

u = ExtractMin(Q);

for each v Adj[u]

if (v Q and w(u,v) < key[v])

p[v] = u;

key[v] = w(u,v);

6

4

9

5

10

2

14

2

10

15

0

8

3

8

3

u

David Luebke 493/10/2014


Prim s algorithm50 l.jpg
Prim’s Algorithm

MST-Prim(G, w, r)

Q = V[G];

for each u Q

key[u] = ;

key[r] = 0;

p[r] = NULL;

while (Q not empty)

u = ExtractMin(Q);

for each v Adj[u]

if (v Q and w(u,v) < key[v])

p[v] = u;

key[v] = w(u,v);

6

4

9

5

10

2

14

2

10

15

0

8

15

3

8

3

u

David Luebke 503/10/2014


Prim s algorithm51 l.jpg
Prim’s Algorithm

u

MST-Prim(G, w, r)

Q = V[G];

for each u Q

key[u] = ;

key[r] = 0;

p[r] = NULL;

while (Q not empty)

u = ExtractMin(Q);

for each v Adj[u]

if (v Q and w(u,v) < key[v])

p[v] = u;

key[v] = w(u,v);

6

4

9

5

10

2

14

2

10

15

0

8

15

3

8

3

David Luebke 513/10/2014


Prim s algorithm52 l.jpg
Prim’s Algorithm

u

MST-Prim(G, w, r)

Q = V[G];

for each u Q

key[u] = ;

key[r] = 0;

p[r] = NULL;

while (Q not empty)

u = ExtractMin(Q);

for each v Adj[u]

if (v Q and w(u,v) < key[v])

p[v] = u;

key[v] = w(u,v);

6

4

9

5

10

2

9

14

2

10

15

0

8

15

3

8

3

David Luebke 523/10/2014


Prim s algorithm53 l.jpg
Prim’s Algorithm

u

MST-Prim(G, w, r)

Q = V[G];

for each u Q

key[u] = ;

key[r] = 0;

p[r] = NULL;

while (Q not empty)

u = ExtractMin(Q);

for each v Adj[u]

if (v Q and w(u,v) < key[v])

p[v] = u;

key[v] = w(u,v);

4

6

4

9

5

10

2

9

14

2

10

15

0

8

15

3

8

3

David Luebke 533/10/2014


Prim s algorithm54 l.jpg
Prim’s Algorithm

u

MST-Prim(G, w, r)

Q = V[G];

for each u Q

key[u] = ;

key[r] = 0;

p[r] = NULL;

while (Q not empty)

u = ExtractMin(Q);

for each v Adj[u]

if (v Q and w(u,v) < key[v])

p[v] = u;

key[v] = w(u,v);

4

6

4

9

5

5

2

9

14

2

10

15

0

8

15

3

8

3

David Luebke 543/10/2014


Prim s algorithm55 l.jpg
Prim’s Algorithm

u

MST-Prim(G, w, r)

Q = V[G];

for each u Q

key[u] = ;

key[r] = 0;

p[r] = NULL;

while (Q not empty)

u = ExtractMin(Q);

for each v Adj[u]

if (v Q and w(u,v) < key[v])

p[v] = u;

key[v] = w(u,v);

4

6

4

9

5

5

2

9

14

2

10

15

0

8

15

3

8

3

David Luebke 553/10/2014


Prim s algorithm56 l.jpg
Prim’s Algorithm

u

MST-Prim(G, w, r)

Q = V[G];

for each u Q

key[u] = ;

key[r] = 0;

p[r] = NULL;

while (Q not empty)

u = ExtractMin(Q);

for each v Adj[u]

if (v Q and w(u,v) < key[v])

p[v] = u;

key[v] = w(u,v);

4

6

4

9

5

5

2

9

14

2

10

15

0

8

15

3

8

3

David Luebke 563/10/2014


Prim s algorithm57 l.jpg
Prim’s Algorithm

u

MST-Prim(G, w, r)

Q = V[G];

for each u Q

key[u] = ;

key[r] = 0;

p[r] = NULL;

while (Q not empty)

u = ExtractMin(Q);

for each v Adj[u]

if (v Q and w(u,v) < key[v])

p[v] = u;

key[v] = w(u,v);

4

6

4

9

5

5

2

9

14

2

10

15

0

8

15

3

8

3

David Luebke 573/10/2014


Prim s algorithm58 l.jpg
Prim’s Algorithm

MST-Prim(G, w, r)

Q = V[G];

for each u Q

key[u] = ;

key[r] = 0;

p[r] = NULL;

while (Q not empty)

u = ExtractMin(Q);

for each v Adj[u]

if (v Q and w(u,v) < key[v])

p[v] = u;

key[v] = w(u,v);

4

6

4

9

5

5

2

9

u

14

2

10

15

0

8

15

3

8

3

David Luebke 583/10/2014


Review prim s algorithm l.jpg
Review: Prim’s Algorithm

MST-Prim(G, w, r)

Q = V[G];

for each u Q

key[u] = ;

key[r] = 0;

p[r] = NULL;

while (Q not empty)

u = ExtractMin(Q);

for each v Adj[u]

if (v Q and w(u,v) < key[v])

p[v] = u;

key[v] = w(u,v);

What is the hidden cost in this code?

David Luebke 593/10/2014


Review prim s algorithm60 l.jpg
Review: Prim’s Algorithm

MST-Prim(G, w, r)

Q = V[G];

for each u Q

key[u] = ;

key[r] = 0;

p[r] = NULL;

while (Q not empty)

u = ExtractMin(Q);

for each v Adj[u]

if (v Q and w(u,v) < key[v])

p[v] = u;

DecreaseKey(v, w(u,v));

David Luebke 603/10/2014


Review prim s algorithm61 l.jpg
Review: Prim’s Algorithm

MST-Prim(G, w, r)

Q = V[G];

for each u Q

key[u] = ;

key[r] = 0;

p[r] = NULL;

while (Q not empty)

u = ExtractMin(Q);

for each v Adj[u]

if (v Q and w(u,v) < key[v])

p[v] = u;

DecreaseKey(v, w(u,v));

How often is ExtractMin() called?

How often is DecreaseKey() called?

David Luebke 613/10/2014


Review prim s algorithm62 l.jpg
Review: Prim’s Algorithm

MST-Prim(G, w, r)

Q = V[G];

for each u Q

key[u] = ;

key[r] = 0;

p[r] = NULL;

while (Q not empty)

u = ExtractMin(Q);

for each v Adj[u]

if (v Q and w(u,v) < key[v])

p[v] = u;

key[v] = w(u,v);

What will be the running time?A: Depends on queue binary heap: O(E lg V) Fibonacci heap: O(V lg V + E)

David Luebke 623/10/2014


Single source shortest path l.jpg
Single-Source Shortest Path

  • Problem: given a weighted directed graph G, find the minimum-weight path from a given source vertex s to another vertex v

    • “Shortest-path” = minimum weight

    • Weight of path is sum of edges

    • E.g., a road map: what is the shortest path from Chapel Hill to Charlottesville?

David Luebke 633/10/2014