CS 225 Data Structures No Novem ember er 16 Gr Graph aph Im - - PowerPoint PPT Presentation

cs 225
SMART_READER_LITE
LIVE PREVIEW

CS 225 Data Structures No Novem ember er 16 Gr Graph aph Im - - PowerPoint PPT Presentation

CS 225 Data Structures No Novem ember er 16 Gr Graph aph Im Implementations and Tr Traversals G G Ca Carl Evans Gr Grap aph ADT Functions: - insertVertex(K key); Data: - insertEdge(Vertex v1, Vertex v2, K key); - Vertices -


slide-1
SLIDE 1

CS 225

Data Structures

No Novem ember er 16 – Gr Graph aph Im Implementations and Tr Traversals

G G Ca Carl Evans

slide-2
SLIDE 2

Gr Grap aph ADT

Functions:

  • insertVertex(K key);
  • insertEdge(Vertex v1, Vertex v2, K key);
  • removeVertex(Vertex v);
  • removeEdge(Vertex v1, Vertex v2);
  • incidentEdges(Vertex v);
  • areAdjacent(Vertex v1, Vertex v2);
  • origin(Edge e);
  • destination(Edge e);

Data:

  • Vertices
  • Edges
  • Some data structure

maintaining the structure between vertices and edges.

X V W Z Y b e d f g h

slide-3
SLIDE 3

Ed Edge List

v u w a c b z d u v w z u v a v w b u w c w z d

Vertex List Edge List

Key Ideas:

  • Given a vertex, O(1) lookup in vertex list
  • Implement w/ a hash table, etc
  • All basic ADT operations runs in O(m) time
slide-4
SLIDE 4

Ad Adjacency M Matri rix

v u w a c b z d u v w z

u v w z u

Ø Ø

v

Ø Ø

w

Ø

z

Ø u v a v w b u w c w z d

Key Ideas:

  • Given a vertex, O(1) lookup in vertex

list

  • Given a pair of vertices (an edge),

O(1) lookup in the matrix

  • Undirected graphs can use an upper

triangular matrix

slide-5
SLIDE 5

Gr Grap aph Imp Implemen ementati tion: Adjac jacen ency List

v u w a c b z d u v w z u v a v w b u w c w z d

slide-6
SLIDE 6

Ad Adjacency L List

v u w a c b z d u v w z u v a v w b u w c w z d a c a b b c d d

d=2 d=2 d=3 d=1

slide-7
SLIDE 7

Ad Adjacency L List

v u w a c b z d u v w z u v a v w b u w c w z d a c a b b c d d

d=2 d=2 d=3 d=1

slide-8
SLIDE 8

Ad Adjacency L List

v u w a c b z d u v w z u v a v w b u w c w z d a c a b b c d d

d=2 d=2 d=3 d=1

insertVertex(K key):

slide-9
SLIDE 9

Ad Adjacency L List

v u w a c b z d u v w z u v a v w b u w c w z d a c a b b c d d

d=2 d=2 d=3 d=1

removeVertex(Vertex v):

slide-10
SLIDE 10

Ad Adjacency L List

v u w a c b z d u v w z u v a v w b u w c w z d a c a b b c d d

d=2 d=2 d=3 d=1

incidentEdges(Vertex v):

slide-11
SLIDE 11

Ad Adjacency L List

v u w a c b z d u v w z u v a v w b u w c w z d a c a b b c d d

d=2 d=2 d=3 d=1

areAdjacent(Vertex v1, Vertex v2):

slide-12
SLIDE 12

Ad Adjacency L List

v u w a c b z d u v w z u v a v w b u w c w z d a c a b b c d d

d=2 d=2 d=3 d=1

insertEdge(Vertex v1, Vertex v2, K key):

slide-13
SLIDE 13

Expressed as O(f)

Edge List Adjacency Matrix Adjacency List Space n+m n2 n+m insertVertex(v) 1 n 1 removeVertex(v) m n deg(v) insertEdge(v, w, k) 1 1 1 removeEdge(v, w) 1 1 1 incidentEdges(v) m n deg(v) areAdjacent(v, w) m 1 min( deg(v), deg(w) )

slide-14
SLIDE 14

mp_traversals and mp_mazes

  • mp_traversals
  • Vertex Set : The pixels are the vertices
  • Edge Set : There is an edge between every n/s/e/w pixel unless the color

change exceeds the tolerance

  • There are several graphs here depending on the tolerance
  • mp_mazes
  • Vertex Set: The squares in the maze are the vertices
  • Edge Set: There is an edge between two vertices if canTravel() returns true
  • Once the maze is made this graph is a spanning tree of the graph with

canTravel() returning true.

slide-15
SLIDE 15

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-16
SLIDE 16

Tr Traversal: BFS

A C D E B F G H

slide-17
SLIDE 17

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-18
SLIDE 18

Tr Traversal: BFS

A C D E B F G H d p Adjacent Edges

0 A A C B D 1 A B A C E 1 A C B A D E F 1 A D A C F H 2 C E B C G 2 C F C D G 3 E G E F H 2 D H D G

G H F E D B C A

slide-19
SLIDE 19

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-20
SLIDE 20

BF BFS An S 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-21
SLIDE 21

Ru Running t time of

  • f BF

BFS

A C D E B F G H d p v Adjacent

0 A A C B D 1 A B A C E 1 A C B A D E F 1 A D A C F H 2 C E B C G 2 C F C D G 3 E G E F H 2 D H D G

G H F E D B C A

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

slide-22
SLIDE 22

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-23
SLIDE 23

BF BFS O S Observation

  • ns

A C D E B F G H d p v Adjacent

0 A A C B D 1 A B A C E 1 A C B A D E F 1 A D A C F H 2 C E B C G 2 C F C D G 3 E G E F H 2 D H D G

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?

slide-24
SLIDE 24

BF BFS O S Observation

  • ns
  • Obs. 1: Traversals can be used to count components.
  • Obs. 2: Traversals 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-25
SLIDE 25

Tr Traversal: DFS

A C D E B F G H J K

slide-26
SLIDE 26

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-27
SLIDE 27

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-28
SLIDE 28

Ru Running t time of

  • f D

DFS

Labeling:

  • Vertex:
  • Edge:

Queries:

  • Vertex:
  • Edge:

A C D E B F G H J K