David Luebke 1 9232009 - PowerPoint PPT Presentation

1 / 47
About This Presentation
Title:

David Luebke 1 9232009

Description:

Tie. Jacket. Getting Dressed. Underwear. Socks. Shoes. Pants. Belt. Shirt. Watch. Tie. Jacket ... Tie. Jacket. Topological Sort Algorithm. Topological-Sort() Run DFS ... – PowerPoint PPT presentation

Number of Views:53
Avg rating:3.0/5.0
Slides: 48
Provided by: david2532
Category:
Tags: david | luebke | tie

less

Transcript and Presenter's Notes

Title: David Luebke 1 9232009


1
CS 332 Algorithms
  • Topological Sort
  • Minimum Spanning Tree

2
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-gtadj
  • if (v-gtcolor WHITE)
  • v-gtcolor GREY
  • v-gtd u-gtd 1
  • v-gtp u
  • Enqueue(Q, v)
  • u-gtcolor BLACK

v-gtd represents depth in tree
v-gtp represents parent in tree
3
Review Depth-First Search
  • Depth-first search is another strategy for
    exploring a graph
  • Explore deeper in the graph whenever possible
  • Edges are explored out of the most recently
    discovered vertex v that still has unexplored
    edges
  • When all of vs edges have been explored,
    backtrack to the vertex from which v was
    discovered

4
Review DFS Code
  • DFS(G)
  • for each vertex u ? G-gtV
  • u-gtcolor WHITE
  • time 0
  • for each vertex u ? G-gtV
  • if (u-gtcolor WHITE)
  • DFS_Visit(u)
  • DFS_Visit(u)
  • u-gtcolor GREY
  • time time1
  • u-gtd time
  • for each v ? u-gtAdj
  • if (v-gtcolor WHITE)
  • DFS_Visit(v)
  • u-gtcolor BLACK
  • time time1
  • u-gtf time

5
DFS Example
sourcevertex
d f
1







6
DFS Example
sourcevertex
d f
1


2




7
DFS Example
sourcevertex
d f
1


2



3
8
DFS Example
sourcevertex
d f
1


2



3 4
9
DFS Example
sourcevertex
d f
1


2


5
3 4
10
DFS Example
sourcevertex
d f
1


2


5 6
3 4
11
DFS Example
sourcevertex
d f
1
8

2 7


5 6
3 4
12
DFS Example
sourcevertex
d f
1
8

2 7


5 6
3 4
13
DFS Example
sourcevertex
d f
1
8

2 7
9

5 6
3 4
What is the structure of the grey vertices?
What do they represent?
14
DFS Example
sourcevertex
d f
1
8

2 7
9 10

5 6
3 4
15
DFS Example
sourcevertex
d f
1
8 11

2 7
9 10

5 6
3 4
16
DFS Example
sourcevertex
d f
1 12
8 11

2 7
9 10

5 6
3 4
17
DFS Example
sourcevertex
d f
1 12
8 11
13
2 7
9 10

5 6
3 4
18
DFS Example
sourcevertex
d f
1 12
8 11
13
2 7
9 10
14
5 6
3 4
19
DFS Example
sourcevertex
d f
1 12
8 11
13
2 7
9 10
1415
5 6
3 4
20
DFS Example
sourcevertex
d f
1 12
8 11
1316
2 7
9 10
1415
5 6
3 4
21
Review Depth-First Sort Analysis
  • Running time O(VE)
  • Show by amortized analysis
  • Charge the exploration of edge to the edge
  • Each loop in DFS_Visit can be attributed to an
    edge in the graph
  • Runs once/edge if directed graph, twice if
    undirected
  • Thus loop will run in O(E) time, algorithm O(VE)
  • Considered linear for graph, b/c adj list
    requires O(VE) storage
  • Important to be comfortable with this kind of
    reasoning and analysis

22
Review Kinds of edges
  • DFS introduces an important distinction among
    edges in the original graph
  • Tree edge encounter new (white) vertex
  • The tree edges form a spanning forest
  • Can tree edges form cycles? Why or why not?

23
Review Kinds of edges
  • DFS introduces an important distinction among
    edges in the original graph
  • Tree edge encounter new (white) vertex
  • Back edge from descendent to ancestor
  • Encounter a grey vertex (grey to grey)

24
Review Kinds of edges
  • DFS introduces an important distinction among
    edges in the original graph
  • Tree edge encounter new (white) vertex
  • Back edge from descendent to ancestor
  • Forward edge from ancestor to descendent
  • Not a tree edge, though
  • From grey node to black node

25
Review Kinds of edges
  • DFS introduces an important distinction among
    edges in the original graph
  • Tree edge encounter new (white) vertex
  • Back edge from descendent to ancestor
  • Forward edge from ancestor to descendent
  • Cross edge between a tree or subtrees
  • From a grey node to a black node

26
Review DFS Example
sourcevertex
d f
1 12
8 11
1316
2 7
9 10
1415
5 6
3 4
Tree edges
Back edges
Forward edges
Cross edges
27
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
  • If acyclic, no back edges
  • If no back edges, acyclic
  • No back edges implies only tree edges (Why?)
  • Only tree edges implies we have a tree or a
    forest
  • Which by definition is acyclic
  • Thus, can run DFS to find cycles

28
DFS And Cycles
  • Running time O(VE)
  • 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 ltV and answer the
    question in constant time?

29
Directed Acyclic Graphs
  • A directed acyclic graph or DAG is a directed
    graph with no directed cycles

30
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 grey?grey, thus (u, v) is a
    back edge

31
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

32
Getting Dressed
Underwear
Socks
Watch
Shoes
Pants
Shirt
Belt
Tie
Jacket
33
Getting Dressed
Underwear
Socks
Watch
Shoes
Pants
Shirt
Belt
Tie
Jacket
Socks
Underwear
Pants
Shoes
Watch
Shirt
Belt
Tie
Jacket
34
Topological Sort Algorithm
  • Topological-Sort()
  • Run DFS
  • When a vertex is finished, output it
  • Vertices are output in reverse topological order
  • Time O(VE)
  • Correctness Want to prove that (u,v) ? G ? u?f
    gt v?f

35
Correctness of Topological Sort
  • Claim (u,v) ? G ? u?f gt v?f
  • When (u,v) is explored, u is grey
  • v grey ? (u,v) is back edge. Contradiction
    (Why?)
  • v white ? v becomes descendent of u ? v?f lt u?f
    (since must finish v before backtracking and
    finishing u)
  • v black ? v already finished ? v?f lt u?f

36
Minimum Spanning Tree
  • Problem given a connected, undirected, weighted
    graph

6
4
5
9
14
2
10
15
3
8
37
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
38
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
39
Minimum Spanning Tree
  • Answer

A
6
4
5
9
H
B
C
14
2
10
15
G
E
D
3
8
F
40
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 cant
    be a better tree than T1 or T2, or T would be
    suboptimal)

41
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

42
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)

43
Prims Algorithm
  • MST-Prim(G, w, r)
  • Q VG
  • for each u ? Q
  • keyu ?
  • keyr 0
  • pr NULL
  • while (Q not empty)
  • u ExtractMin(Q)
  • for each v ? Adju
  • if (v ? Q and w(u,v) lt keyv)
  • pv u
  • keyv w(u,v)

44
Prims Algorithm
  • MST-Prim(G, w, r)
  • Q VG
  • for each u ? Q
  • keyu ?
  • keyr 0
  • pr NULL
  • while (Q not empty)
  • u ExtractMin(Q)
  • for each v ? Adju
  • if (v ? Q and w(u,v) lt keyv)
  • pv u
  • keyv w(u,v)

6
4
9
5
14
2
10
15
3
8
Run on example graph
45
Prims Algorithm
  • MST-Prim(G, w, r)
  • Q VG
  • for each u ? Q
  • keyu ?
  • keyr 0
  • pr NULL
  • while (Q not empty)
  • u ExtractMin(Q)
  • for each v ? Adju
  • if (v ? Q and w(u,v) lt keyv)
  • pv u
  • keyv w(u,v)

What will be the running time?
46
Prims Algorithm
  • MST-Prim(G, w, r)
  • Q VG
  • for each u ? Q
  • keyu ?
  • keyr 0
  • pr NULL
  • while (Q not empty)
  • u ExtractMin(Q)
  • for each v ? Adju
  • if (v ? Q and w(u,v) lt keyv)
  • pv u
  • keyv 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)
47
The End
Write a Comment
User Comments (0)
About PowerShow.com