Graph Traversals CS200 - Graphs 1 Tree traversal reminder Pre - - PowerPoint PPT Presentation

graph traversals
SMART_READER_LITE
LIVE PREVIEW

Graph Traversals CS200 - Graphs 1 Tree traversal reminder Pre - - PowerPoint PPT Presentation

Graph Traversals CS200 - Graphs 1 Tree traversal reminder Pre order A A B D G H C E F I In order B C G D H B A E C F I Post order D E F G H D B E I F C A Level order G H I A B C D E F G H I Connected Components n The connected


slide-1
SLIDE 1

Graph Traversals

CS200 - Graphs 1

slide-2
SLIDE 2

Tree traversal reminder

A B D G C E H F I Pre order A B D G H C E F I In order G D H B A E C F I Post order G H D B E I F C A Level order A B C D E F G H I

slide-3
SLIDE 3

Connected Components

n The connected component of a node s is the largest set of nodes

reachable from s. A generic algorithm for creating connected component(s):

n Upon termination, R is the connected component containing s.

q Breadth First Search (BFS): explore in order of distance from s. q Depth First Search (DFS): explores edges from the most

recently discovered node; backtracks when reaching a dead- end.

3

R = {s} while add v to R

∃edge(u,v): u ∈ R∧v ∉ R

slide-4
SLIDE 4

Graph Traversals – Depth First Search

n Depth First Search starting at u

CS200 - Graphs 4

DFS(u): mark u as visited and add u to R for each edge (u,v) : if v is not marked visited : DFS(v)

slide-5
SLIDE 5

Depth First Search

A B C D E F G H I J K L M N O P

CS200 - Graphs 5

slide-6
SLIDE 6

Question

n What determines the order in which DFS

visits nodes?

n The order in which a node picks its outgoing

edges

CS200 - Graphs 6

slide-7
SLIDE 7

Graph Traversal

Depth First Search (DFS)

dfs(in v:Vertex)

mark v as visited for (each unvisited vertex u adjacent to v) dfs(u)

n Need to track visited nodes n Order of visiting nodes is not completely specified

q if nodes have priority, then the order may become deterministic

for (each unvisited vertex u adjacent to v in priority order) n DFS applies to both directed and undirected graphs n Which graph implementation is suitable?

Depth first search algorithm

CS200 - Graphs 7

slide-8
SLIDE 8

Iterative DFS: explicit Stack

dfs(in v:Vertex) s – stack for keeping track of active vertices s.push(v) mark v as visited while (!s.isEmpty()) { if (no unvisited vertices adjacent to the vertex on top of the stack) { s.pop() //backtrack else { select unvisited vertex u adjacent to vertex on top of the stack s.push(u) mark u as visited } }

CS200 - Graphs 8

slide-9
SLIDE 9

Breadth First Search (BFS)

n Is like level order in

trees

n Which is a BFS

traversal starting from A?

  • A. A, B, C, D, …
  • B. A, B, F, E, …
  • C. A, E, F, B, …
  • D. A, B, E, F, …

CS200 - Graphs

A B C D E F G H I J K L M N O P

9

slide-10
SLIDE 10

5 4 1 2 3

Graph Traversal – Breadth First Search (BFS)

A B C D E F G H I J K L M N O P

Brea eadth Fi First Sea earch

CS200 - Graphs 10

slide-11
SLIDE 11

BFS

n Similar to level order tree traversal

n DFS is a last visited first explored strategy

(uses a stack)

n BFS is a first visited first explored strategy

(uses a queue)

CS200 - Graphs 11

slide-12
SLIDE 12

BFS

bfs(in v:Vertex) q – queue of nodes to be processed q.enque(v) mark v as visited while(!q.isEmpty()) { w = q.dequeue() for (each unvisited vertex u adjacent to w) { mark u as visited q.enqueue(u) } }

CS200 - Graphs 12

slide-13
SLIDE 13

Trace this example

5 4 1 2 3 A B C D E F G H I J K L M N O P

bfs(in v:Vertex) q – queue of nodes q.enque(v) mark v as visited while(!q.isEmpty()) { w = q.dequeue() for (each unvisited vertex u adjacent to w) { mark u as visited q.enqueue(u) } }

CS200 - Graphs 13

slide-14
SLIDE 14

Graph Traversal

n Properties of BFS and DFS:

q Visit all vertices that are reachable from a given

vertex

q Therefore DFS(v) and BFS(v) visit a connected

component

n Computation time for DFS, BFS for a

connected graph: O(|V| + |E|) WHY?

CS200 - Graphs 14

slide-15
SLIDE 15

Complexity BFS / DFS

n Each node is marked at most once, and visited at most

  • nce.

n The adjacency list of each node is scanned only once. n Therefore time complexity for BFS and DFS is

O(|V|+|E|) or O(n+m)

slide-16
SLIDE 16

Reachability

n Reachability

q v is reachable from u

if there is a (directed) path from u to v

q solved using BFS or DFS

n Transitive Closure (G*)

q G* has edge from u to v

if v is reachable from u

CS200 - Graphs 16

A B C D E

Do it for:

slide-17
SLIDE 17

Trees as Graphs

n Tree: an undirected connected graph that

has no cycles.

A B C D E F G H I J K L M N O P

CS200 - Graphs 17

slide-18
SLIDE 18

Rooted Trees

n A rooted tree is a tree in which one vertex

has been designated as the root and every edge is directed away from the root

CS200 - Graphs 18

slide-19
SLIDE 19

Example: Build rooted trees.

A B C D E F G H I J K L M N O P Question: Which node CANNOT be a root of this tree?

  • A. Node E B. Node G C. Node D D. None

CS200 - Graphs 19

slide-20
SLIDE 20

Trees as Graphs

n Tree: an undirected connected graph that

has no simple cycle.

n Cycle: a path that begins and ends at the

same vertex and has length > 0

n Simple cycle: does not contain the same

edge more than once

CS200 - Graphs 20

slide-21
SLIDE 21

Theorems

A connected undirected graph with n vertices must have at least n-1 edges (otherwise some node is isolated.) In a tree there is a unique path (no repeated nodes) between any two nodes (go up to common parent, go down to

  • ther node.)

A connected graph with n-1 edges is a tree. If we add one edge to a tree it gets a cycle, because there are then two paths between the incident nodes

CS200 - Graphs 21

A B C D E F G H I J K L M N O P

slide-22
SLIDE 22

Spanning Trees

n Spanning tree: A sub-graph of a connected

undirected graph G that contains all of G’s vertices and enough of its edges to form a tree.

n How to get a spanning tree:

q From the whole graph, remove edges until you get a

tree, never disconnecting the nodes in the tree

q From the set of nodes, add edges until you have a

spanning tree, never creating a cycle

CS200 - Graphs 22

slide-23
SLIDE 23

Spanning Trees - DFS algorithm

dfsTree(in v:vertex) Mark v as visited for (each unvisited vertex u adjacent to v) Mark the edge from u to v dfsTree(u)

CS200 - Graphs 23

slide-24
SLIDE 24

Spanning Tree –

Depth First Search Example

A B C D E F G H I J K L M N O P

CS200 - Graphs 24

slide-25
SLIDE 25

Example

n

Suppose that an airline must reduce its flight schedule to save money. If its

  • riginal routes are as illustrated here, which flights can be discontinued to

retain service between all pairs of cities (where might it be necessary to combine flights to fly from one city to another?)

Seattle Chicago Detroit SF LA San Diego Denver Dallas

  • St. Louis

Atlanta Washington D.C. NYC Boston Bangor

CS200 - Graphs 25

slide-26
SLIDE 26

Question

n Does Dijkstra’s algorithm lead to the

spanning tree with the minimum total distance?

n No.

CS200 - Graphs 26

slide-27
SLIDE 27

Question

n Does Dijkstra’s algorithm lead to the

spanning tree with the minimum total distance? Dijkstra determines the shortest path from a source to each node in the graph

n No.

Counter example: (s=A) Shortest paths from A? Minimal total distance spanning tree?

CS200 - Graphs 27

A B C 4 2 3

slide-28
SLIDE 28

Minimum Spanning Tree

n Minimum spanning tree

q Spanning tree minimizing the sum of edge

weights

n Example: Connecting each house in the

neighborhood to cable

q Graph where each house is a vertex. q Need the graph to be connected, and minimize the

cost of laying the cables.

CS200 - Graphs 28

slide-29
SLIDE 29

Prim’s Algorithm

n Idea: incrementally build spanning tree by

adding the least-cost edge to the tree

q Weighted graph q Find a set of edges

n Touches all vertices n Minimal weight n Not all the edges may be used

CS200 - Graphs 29

slide-30
SLIDE 30

g e f d i c b a h 4 8 7 9 10 7 4 2 11 8 7 1 2 6 g d f e i c b h a

Prim’s Algorithm: Example starting at d

{(d,c),(c,b), (b,i), (b,e), (e,f), (f,g), (g,h), (h,a) }

CS200 - Graphs 30

unique?

slide-31
SLIDE 31

Prim’s Algorithm

prims(in v:Vertex)

// Determines a minimum spanning tree for a weighted // connected, undirected graph whose weights are // nonnegative, beginning with any vertex v.

Mark vertex v as visited and include it in the minimum spanning tree while (there are unvisited vertices) {

find the least-cost edge (v, u) from a visited vertex v to some unvisited vertex u Mark u as visited Add vertex u and the edge (v, u) to the minimum spanning tree

}

return minimum spanning tree

CS200 - Graphs 31

slide-32
SLIDE 32

Prim vs Dijkstra

n Prim’s MST algorithm is very similar to

Dijkstra’s SSSP algorithm.

n What is the difference?

CS200 - Graphs 32

slide-33
SLIDE 33

CS200 - Class Overview 33

S 7 9 8 5 7 5 15 6 8 9 11 Do Prim’s Minimum Spanning Tree Algorithm, Source: S Draw MST Do Dijkstra’s Shortest Path algorithm, source S Draw MPT

slide-34
SLIDE 34

Graphs Describing Precedence

n Edge from x to y indicates x should come before y,

e.g.:

q prerequisites for a set of courses q dependences between programs q dependences between statements

a = 10 b = 20 c = a+b

q set of tasks

CS200 - Graphs 34

slide-35
SLIDE 35

Graphs Describing Precedence

Batman images are from the book “Introduction to bioinformatics algorithms”

CS200 - Graphs 35

slide-36
SLIDE 36

Graphs Describing Precedence

n Want an ordering of the vertices of the graph

that respects the precedence relation

q Example: An ordering of CS courses

n The graph must not contain cycles. WHY?

CS200 - Graphs 36

slide-37
SLIDE 37

CS161 CS200 CS253 CS270 CS314 CS320 CS356 CS370 CT310 CT320 CS440 CS410 CS464 CS160

CS Courses Required for CS and ACT Majors Question Is there a cycle in this graph?

  • A. Yes
  • B. No
slide-38
SLIDE 38

Topological Sorting of DAGs

n DAG: Directed Acyclic Graph n Topological sort: listing of nodes such that if (a,b)

is an edge, a appears before b in the list

n Is a topological sort unique?

Question: Is a topological sort unique?

CS200 - Graphs 38

slide-39
SLIDE 39

A directed graph without cycles

a b c d e f g

a,g,d,b,e,c,f a,b,g,d,e,f,c

CS200 - Graphs 39

slide-40
SLIDE 40

Topological Sort - Algorithm 1

topSort1(in G:Graph) n= number of vertices in G for (step =1 through n) select a vertex v that has no successors aList.add(0,v) Delete from G vertex v and its edges return aList

  • Algorithm relies on the fact that in a DAG there is always

a vertex that has no successors.

  • Destructively modifies the graph.

CS200 - Graphs 40

slide-41
SLIDE 41

Topological Sort - Algorithm 1

topSort1(in G:Graph) n= number of vertices in G for (step =1 through n) select a vertex v that has no successors aList.add(0,v) Delete from G vertex v and its edges return aList

a b c d e f g f c e b d g a

CS200 - Graphs 41

slide-42
SLIDE 42

Algorithm 2: Example 2

B C E F G H I

I H, F, C, G, B, D, A,

CS200 - Graphs 42

E,

slide-43
SLIDE 43

Topological Sort - Algorithm 2

n Modification of DFS: Traverse tree using

DFS starting from all nodes that have no predecessor.

n Add a node to the list when ready to

backtrack.

CS200 - Graphs 43

slide-44
SLIDE 44

Topological Sort - Algorithm 2

topSort2( in theGraph:Graph):List s.createStack() for (all vertices v in the graph theGraph) if (v has no predecessors) s.push(v) Mark v as visited while (!s.isEmpty()) if (all vertices adjacent to the vertex on top of the stack have been visited) v = s.pop() aList.add(0, v) else Select an unvisited vertex u adjacent to vertex on top

  • f the stack

s.push(u) Mark u as visited return aList

CS200 - Graphs 44

slide-45
SLIDE 45

Algorithm 2: Example 1

a b c d e f g f c e b d g a f c e b d g a

CS200 - Graphs 45

slide-46
SLIDE 46

Topological sorting solution

A B C D E F G H I 1/18 10/15 11/14 12/13 9/16 2/17 6/7 3/8 4/5 Red edges represent spanning tree

CS200 - Graphs 46

slide-47
SLIDE 47

Third topological sort algorithm

n First two topological sort algorithms found nodes without

successors and then backtracked

n Forward algorithm based on inDegrees

q Copy all inDegrees to temporary inDegree inCount q Repeat until all visited: 1.

Find new nodes without predecessors (inCount 0)

2.

Put these in a list, or print them out (P5), making sure they will not be selected again (e.g. set their inCount to -1)

3.

Subtract 1 from inCount of all successors of the nodes from step 2

CS200 - Graphs 47

slide-48
SLIDE 48

forward topological sort

CS200 - Graphs 48

A B C D E F G H 0,0 1,1 1,1 1,1 2,2 2,2 1,1 2,2 A B C D E F G H 0,-1 1,0 1,1 1,0 2,2 2,2 1,1 2,2 A A B C D E F G H 0,-1 1,-1 1,0 1,-1 2,2 2,1 1,0 2,1 B D A B C D E F G H 0,-1 1,-1 1,-1 1,-1 2,2 2,0 1,-1 2,0 C E

finish the animation

slide-49
SLIDE 49

forward topological sort

CS200 - Graphs 49

A B D A B C D E F G H 0,-1 1,-1 1,-1 1,-1 2,2 2,0 1,-1 2,0 C E A B C D E F G H 0,-1 1,-1 1,-1 1,-1 2,0 2,-1 1,-1 2,-1 F G A B C D E F G H 0,-1 1,-1 1,-1 1,-1 2,-1 2,-1 1,-1 2,-1 H