CS 225 Data Structures No Novem ember er 15 Gr Graph aph Trav - - PowerPoint PPT Presentation

cs 225
SMART_READER_LITE
LIVE PREVIEW

CS 225 Data Structures No Novem ember er 15 Gr Graph aph Trav - - PowerPoint PPT Presentation

CS 225 Data Structures No Novem ember er 15 Gr Graph aph Trav aversal als G G Carl Evans Gr Grap aphs To study all of these structures: 1. A common vocabulary 2. Graph implementations 3. Graph traversals 4. Graph algorithms


slide-1
SLIDE 1

CS 225

Data Structures

No Novem ember er 15 – Gr Graph aph Trav aversal als

G G Carl Evans

slide-2
SLIDE 2

Gr Grap aphs

To study all of these structures:

  • 1. A common vocabulary
  • 2. Graph implementations
  • 3. Graph traversals
  • 4. Graph algorithms
slide-3
SLIDE 3

Tr Traversal:

Objective: Visit every vertex and every edge in the graph. Purpose: Search for interesting sub-structures in the graph. We’ve seen traversal before ….but it’s different:

  • Ordered
  • Obvious Start
slide-4
SLIDE 4

Tr Traversal: BFS

A C D E B F G H

slide-5
SLIDE 5

Tr Traversal: BFS

A C D E B F G H v d P Adjacent Edges

A B C D E F G H

slide-6
SLIDE 6

Tr Traversal: BFS

A C D E B F G H

G H F E D B C A

v d P Adjacent Edges

A 0 -

C B D

B 1 A

A C E

C 1 A

B A D E F

D 1 A

A C F H

E 2 C

B C G

F 2 C

C D G

G 3 E

E F H

H 2 D D G

slide-7
SLIDE 7

BFS(G): Input: Graph, G Output: A labeling of the edges on G as discovery and cross edges foreach (Vertex v : G.vertices()): setLabel(v, UNEXPLORED) foreach (Edge e : G.edges()): setLabel(e, UNEXPLORED) foreach (Vertex v : G.vertices()): if getLabel(v) == UNEXPLORED: BFS(G, v) 1 2 3 4 5 6 7 8 9 10 11 12 BFS(G, v): Queue q setLabel(v, VISITED) q.enqueue(v) while !q.empty(): v = q.dequeue() foreach (Vertex w : G.adjacent(v)): if getLabel(w) == UNEXPLORED: setLabel(v, w, DISCOVERY) setLabel(w, VISITED) q.enqueue(w) elseif getLabel(v, w) == UNEXPLORED: setLabel(v, w, CROSS) 14 15 16 17 18 19 20 21 22 23 24 25 26 27

slide-8
SLIDE 8

BF BFS S An Analysis

Q: Does our implementation handle disjoint graphs? If so, what code handles this?

  • How do we use this to count components?

Q: Does our implementation detect a cycle?

  • How do we update our code to detect a cycle?

Q: What is the running time?

slide-9
SLIDE 9

BFS(G): Input: Graph, G Output: A labeling of the edges on G as discovery and cross edges foreach (Vertex v : G.vertices()): setLabel(v, UNEXPLORED) foreach (Edge e : G.edges()): setLabel(e, UNEXPLORED) foreach (Vertex v : G.vertices()): if getLabel(v) == UNEXPLORED: BFS(G, v) 1 2 3 4 5 6 7 8 9 10 11 12 BFS(G, v): Queue q setLabel(v, VISITED) q.enqueue(v) while !q.empty(): v = q.dequeue() foreach (Vertex w : G.adjacent(v)): if getLabel(w) == UNEXPLORED: setLabel(v, w, DISCOVERY) setLabel(w, VISITED) q.enqueue(w) elseif getLabel(v, w) == UNEXPLORED: setLabel(v, w, CROSS) 14 15 16 17 18 19 20 21 22 23 24 25 26 27

slide-10
SLIDE 10

Running g time of BFS

A C D E B F G H

G H F E D B C A

While-loop at :19? For-loop at :21?

v d P Adjacent Edges

A 0 -

C B D

B 1 A

A C E

C 1 A

B A D E F

D 1 A

A C F H

E 2 C

B C G

F 2 C

C D G

G 3 E

E F H

H 2 D D G

slide-11
SLIDE 11

BF BFS S Observation

  • ns

A C D E B F G H

Q: What is a shortest path from A to H? Q: What is a shortest path from E to H? Q: How does a cross edge relate to d? Q: What structure is made from discovery edges?

v d P Adjacent Edges

A 0 -

C B D

B 1 A

A C E

C 1 A

B A D E F

D 1 A

A C F H

E 2 C

B C G

F 2 C

C D G

G 3 E

E F H

H 2 D D G

slide-12
SLIDE 12

BF BFS S Observation

  • ns
  • Obs. 1: BFS can be used to count components.
  • Obs. 2: BFS can be used to detect cycles.
  • Obs. 3: In BFS, d provides the shortest distance to

every vertex.

  • Obs. 4: In BFS, the endpoints of a cross edge never

differ in distance, d, by more than 1: |d(u) - d(v)| = 1

slide-13
SLIDE 13

Tr Traversal: DFS

A C D E B F G H J K

slide-14
SLIDE 14

BFS(G): Input: Graph, G Output: A labeling of the edges on G as discovery and cross edges foreach (Vertex v : G.vertices()): setLabel(v, UNEXPLORED) foreach (Edge e : G.edges()): setLabel(e, UNEXPLORED) foreach (Vertex v : G.vertices()): if getLabel(v) == UNEXPLORED: BFS(G, v) 1 2 3 4 5 6 7 8 9 10 11 12 BFS(G, v): Queue q setLabel(v, VISITED) q.enqueue(v) while !q.empty(): v = q.dequeue() foreach (Vertex w : G.adjacent(v)): if getLabel(w) == UNEXPLORED: setLabel(v, w, DISCOVERY) setLabel(w, VISITED) q.enqueue(w) elseif getLabel(v, w) == UNEXPLORED: setLabel(v, w, CROSS) 14 15 16 17 18 19 20 21 22 23 24 25 26 27

slide-15
SLIDE 15

DFS(G): Input: Graph, G Output: A labeling of the edges on G as discovery and back edges foreach (Vertex v : G.vertices()): setLabel(v, UNEXPLORED) foreach (Edge e : G.edges()): setLabel(e, UNEXPLORED) foreach (Vertex v : G.vertices()): if getLabel(v) == UNEXPLORED: DFS(G, v) 1 2 3 4 5 6 7 8 9 10 11 12 DFS(G, v): Queue q setLabel(v, VISITED) q.enqueue(v) while !q.empty(): v = q.dequeue() foreach (Vertex w : G.adjacent(v)): if getLabel(w) == UNEXPLORED: setLabel(v, w, DISCOVERY) setLabel(w, VISITED) DFS(G, w) elseif getLabel(v, w) == UNEXPLORED: setLabel(v, w, BACK) 14 15 16 17 18 19 20 21 22 23 24 25 26 27

slide-16
SLIDE 16

Tr Traversal: DFS

A C D E B F G H J K

slide-17
SLIDE 17

Tr Traversal: DFS

A C D E B F G H J K

Discovery Edge Back Edge

slide-18
SLIDE 18

BFS(G): Input: Graph, G Output: A labeling of the edges on G as discovery and cross edges foreach (Vertex v : G.vertices()): setLabel(v, UNEXPLORED) foreach (Edge e : G.edges()): setLabel(e, UNEXPLORED) foreach (Vertex v : G.vertices()): if getLabel(v) == UNEXPLORED: BFS(G, v) 1 2 3 4 5 6 7 8 9 10 11 12 BFS(G, v): Queue q setLabel(v, VISITED) q.enqueue(v) while !q.empty(): v = q.dequeue() foreach (Vertex w : G.adjacent(v)): if getLabel(w) == UNEXPLORED: setLabel(v, w, DISCOVERY) setLabel(w, VISITED) q.enqueue(w) elseif getLabel(v, w) == UNEXPLORED: setLabel(v, w, CROSS) 14 15 16 17 18 19 20 21 22 23 24 25 26 27

slide-19
SLIDE 19

DFS(G): Input: Graph, G Output: A labeling of the edges on G as discovery and back edges foreach (Vertex v : G.vertices()): setLabel(v, UNEXPLORED) foreach (Edge e : G.edges()): setLabel(e, UNEXPLORED) foreach (Vertex v : G.vertices()): if getLabel(v) == UNEXPLORED: DFS(G, v) 1 2 3 4 5 6 7 8 9 10 11 12 DFS(G, v): Queue q setLabel(v, VISITED) q.enqueue(v) while !q.empty(): v = q.dequeue() foreach (Vertex w : G.adjacent(v)): if getLabel(w) == UNEXPLORED: setLabel(v, w, DISCOVERY) setLabel(w, VISITED) DFS(G, w) elseif getLabel(v, w) == UNEXPLORED: setLabel(v, w, BACK) 14 15 16 17 18 19 20 21 22 23 24 25 26 27

slide-20
SLIDE 20

Running g time of DFS

Labeling:

  • Vertex:
  • Edge:

Queries:

  • Vertex:
  • Edge:

A C D E B F G H J K

slide-21
SLIDE 21

“The Muddy City” by CS Unplugged, Creative Commons BY-NC-SA 4.0

slide-22
SLIDE 22

Minimum Spanning g Tree Algorithms

Input: Connected, undirected graph G with edge weights (unconstrained, but must be additive) Output: A graph G’ with the following properties:

  • G’ is a spanning graph of G
  • G’ is a tree (connected, acyclic)
  • G’ has a minimal total weight among all spanning

trees

A C D E B F 8 4 2 7 1 2 3 9 5

slide-23
SLIDE 23

Kr Kruskal’s Algorithm

A C D E B F G H 16 5 5 2 15 16 10 11 12 2 8 9 4 12 17 13 (A, D) (E, H) (F, G) (A, B) (B, D) (G, E) (G, H) (E, C) (C, H) (E, F) (F, C) (D, E) (B, C) (C, D) (A, F) (D, F)

slide-24
SLIDE 24

Kr Kruskal’s Algorithm

A C D E B F G H 16 5 5 2 15 16 10 11 12 2 8 9 4 12 17 13 (A, D) (E, H) (F, G) (A, B) (B, D) (G, E) (G, H) (E, C) (C, H) (E, F) (F, C) (D, E) (B, C) (C, D) (A, F) (D, F) A C D E B F G H

slide-25
SLIDE 25

Kr Kruskal’s Algorithm

A C D E B F G H 16 5 5 2 15 16 10 11 12 2 8 9 4 12 17 13

KruskalMST(G): DisjointSets forest foreach (Vertex v : G): forest.makeSet(v) PriorityQueue Q // min edge weight foreach (Edge e : G): Q.insert(e) Graph T = (V, {}) while |T.edges()| < n-1: Vertex (u, v) = Q.removeMin() if forest.find(u) == forest.find(v): T.addEdge(u, v) forest.union( forest.find(u), forest.find(v) ) return T 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

(A, D) (E, H) (F, G) (A, B) (B, D) (G, E) (G, H) (E, C) (C, H) (E, F) (F, C) (D, E) (B, C) (C, D) (A, F) (D, F) A C D E B F G H

slide-26
SLIDE 26

Kr Kruskal’s Algorithm

KruskalMST(G): DisjointSets forest foreach (Vertex v : G): forest.makeSet(v) PriorityQueue Q // min edge weight foreach (Edge e : G): Q.insert(e) Graph T = (V, {}) while |T.edges()| < n-1: Vertex (u, v) = Q.removeMin() if forest.find(u) == forest.find(v): T.addEdge(u, v) forest.union( forest.find(u), forest.find(v) ) return T 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

Priority Queue: Heap Sorted Array Building :7-9 Each removeMin :13

slide-27
SLIDE 27

Kr Kruskal’s Algorithm

KruskalMST(G): DisjointSets forest foreach (Vertex v : G): forest.makeSet(v) PriorityQueue Q // min edge weight foreach (Edge e : G): Q.insert(e) Graph T = (V, {}) while |T.edges()| < n-1: Vertex (u, v) = Q.removeMin() if forest.find(u) == forest.find(v): T.addEdge(u, v) forest.union( forest.find(u), forest.find(v) ) return T 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

Priority Queue: Total Running Time Heap Sorted Array