Graph Traversal Section 4.14.2 Dr. Mayfield and Dr. Lam Department - - PowerPoint PPT Presentation

graph traversal
SMART_READER_LITE
LIVE PREVIEW

Graph Traversal Section 4.14.2 Dr. Mayfield and Dr. Lam Department - - PowerPoint PPT Presentation

Graph Traversal Section 4.14.2 Dr. Mayfield and Dr. Lam Department of Computer Science James Madison University Oct 30, 2015 Reminder Portfolio 7 due in two weeks Submit four new problems (seven total) You may replace/improve


slide-1
SLIDE 1

Graph Traversal

Section 4.1–4.2

  • Dr. Mayfield and Dr. Lam

Department of Computer Science James Madison University

Oct 30, 2015

slide-2
SLIDE 2

Reminder

“Portfolio 7” due in two weeks

◮ Submit four new problems (seven total) ◮ You may replace/improve the other three

See Port3 feedback in Canvas!

◮ Avoid problems from the first three weeks ◮ Double check formatting, style, spacing, etc.

Oct 30, 2015 Graph Traversal 2 of 18

slide-3
SLIDE 3

LaTeX tips

◮ Use dollar signs for math mode (use ✩n✩ for n) ◮ If you want to say nth occurrence: ✩n^{th}✩ ◮ Use tilde for abbreviations (e.g., Dr.∼Mayfield) ◮ Add tabsize=4 to lstdefinestyle (or use spaces) ◮ Use -- for – (n-dash) and --- for — (m-dash) ◮ Curvy quotes are ‘‘different’’ on the left/right ◮ lstlisting style should match the language ◮ Don’t forget to change the date (on first page)

Oct 30, 2015 Graph Traversal 3 of 18

slide-4
SLIDE 4

Section 4.1–4.2

The following slides are by Steven Halim https://sites.google.com/site/stevenhalim/home/material

slide-5
SLIDE 5

Graph Terms Quick Review Graph Terms – Quick Review

/ – Vertices/Nodes – Edges – (Strongly) Connected Component S b G h – Un/Weighted – Un/Directed – Sub Graph – Complete Graph Di d A li G h – In/Out Degree – Self‐Loop/Multiple Edges ( l h) l – Directed Acyclic Graph – Tree/Forest l / l (Multigraph) vs Simple Graph S /D – Euler/Hamiltonian Path/Cycle Bi tit G h – Sparse/Dense – Path, Cycle I l t d R h bl – Bipartite Graph – Isolated, Reachable

CS3233 ‐ Competitive Programming, Steven Halim, SoC, NUS

slide-6
SLIDE 6

Depth‐First Search (DFS) Breadth‐First Search (BFS)

Reachability Finding Connected Components Finding Connected Components Flood Fill Topological Sort Finding Cycles (Back Edges) Finding Articulation Points & Bridges Finding Strongly Connected Components

GRAPH TRAVERSAL ALGORITHMS

Finding Strongly Connected Components

CS3233 ‐ Competitive Programming, Steven Halim, SoC, NUS

slide-7
SLIDE 7

Graph Traversal Algorithms Graph Traversal Algorithms

  • Given a graph, we want to traverse it!
  • There are 2 major ways:

There are 2 major ways:

– Depth First Search (DFS)

U ll i l t d i i

  • Usually implemented using recursion
  • More natural

M f l d h

  • Most frequently used to traverse a graph

– Breadth First Search (BFS)

  • Usually implemented using queue (+ map), use STL
  • Can solve special case* of “shortest paths” problem!

CS3233 ‐ Competitive Programming, Steven Halim, SoC, NUS

slide-8
SLIDE 8

Depth First Search Template Depth First Search – Template

  • O(V + E) if using Adjacency List
  • O(V2) if using Adjacency Matrix

typedef pair<int, int> ii; typedef vector<ii> vi; void dfs(int u) { // DFS for normal usage printf(" %d", u); // this vertex is visited dfs_num[u] = DFS_BLACK; // mark as visited for (int j = 0; j < (int)AdjList[u].size; j++) { ii v = AdjList[u][j]; // try all neighbors v of vertex u if (dfs_num[v.first] == DFS_WHITE) // avoid cycle dfs(v.first); // v is a (neighbor, weight) pair } }

CS3233 ‐ Competitive Programming, Steven Halim, SoC, NUS

slide-9
SLIDE 9

Breadth First Search (using STL) Breadth First Search (using STL)

  • Complexity: also O(V + E) using Adjacency List

<i t i t> di t di t[ ] map<int, int> dist; dist[source] = 0; queue<int> q; q.push(source); // start from source hil (! t ()) { while (!q.empty()) { int u = q.front(); q.pop(); // queue: layer by layer! for (int j = 0; j < (int)AdjList[u].size(); j++) { ii AdjLi t[ ][j] // f h i hb f ii v = AdjList[u][j]; // for each neighbours of u if (!dist.count(v.first)) { dist[v.first] = dist[u] + 1; // unvisited + reachable h( fi t) // fi t f t t q.push(v.first); // enqueue v.first for next steps } } }

CS3233 ‐ Competitive Programming, Steven Halim, SoC, NUS

slide-10
SLIDE 10

1st Application: Connected Components 1st Application: Connected Components

  • DFS (and BFS) can find connected components

– A call of dfs(u) visits only vertices connected to u ( ) y

int numComp = 0; dfs_num.assign(V, DFS_WHITE); REP (i, 0, V - 1) // for each vertex i in [0..V-1] if (dfs num[i] == DFS WHITE) { // if not visited yet if (dfs_num[i] == DFS_WHITE) { // if not visited yet printf("Component %d, visit", ++numComp); dfs(i); // one component found printf("\n"); } printf("There are %d connected components\n", numComp); p ( p , p);

CS3233 ‐ Competitive Programming, Steven Halim, SoC, NUS

slide-11
SLIDE 11

Graph Traversal Comparison Graph Traversal Comparison

  • DFS
  • Pros:
  • BFS
  • Pros:

Pros:

– Slightly easier to code

Pros:

– Can solve SSSP on unweighted graphs code – Use less memory unweighted graphs (discussed later)

  • Cons:

– Cannot solve SSSP on

  • Cons:

– Slightly longer to Cannot solve SSSP on unweighted graphs g y g code – Use more memory Use more memory

CS3233 ‐ Competitive Programming, Steven Halim, SoC, NUS

slide-12
SLIDE 12

BFS (unweighted) Dijk t ’ ( l ) Dijkstra’s (non –ve cycle) Bellman Ford’s (may have –ve cycle), not discussed Floyd Warshall’s (all‐pairs

SHORTEST PATHS

Floyd Warshall s (all pairs

CS3233 ‐ Competitive Programming, Steven Halim, SoC, NUS

slide-13
SLIDE 13

BFS for Special Case SSSP BFS for Special Case SSSP

  • SSSP is a classical problem in Graph theory:

– Find shortest paths from one source to the rest^ p

  • Special case: UVa 336 (A Node Too Far)
  • Problem Description:

– Given an un‐weighted & un‐directed Graph, g p , a starting vertex v, and an integer TTL – Check how many nodes are un‐reachable from v Check how many nodes are un reachable from v

  • r has distance > TTL from v
  • i e length(shortest path(v node)) > TTL

i.e. length(shortest_path(v, node)) > TTL

CS3233 ‐ Competitive Programming, Steven Halim, SoC, NUS

slide-14
SLIDE 14

Example (1)

1 2 3 4 7 6 5 8

Q = {5} D[5] = 0

9 10 11 12 5

slide-15
SLIDE 15

1 2 3

Example (2)

4 7 6 5 8

Q = {5} Q = {1, 6, 10} D[5] = 0 D[1] = D[5] + 1 = 1

9 10 11 12

D[1] = D[5] + 1 = 1 D[6] = D[5] + 1 = 1 D[10] = D[5] + 1 = 1

1 5 6 10

slide-16
SLIDE 16

1 2 3

Example (3)

4 7 6 5 8

Q = {5} Q = {1, 6, 10} Q = {6 10 0 2} D[5] = 0 D[1] = D[5] + 1 = 1

9 10 11 12

Q = {6, 10, 0, 2} Q = {10, 0, 2, 11} Q = {0, 2, 11, 9} D[1] = D[5] + 1 = 1 D[6] = D[5] + 1 = 1 D[10] = D[5] + 1 = 1 D[0] = D[1] + 1 = 2

1 20 2

D[0] = D[1] + 1 = 2 D[2] = D[1] + 1 = 2 D[11] = D[6] + 1 = 2 D[9] = D[10] + 1 = 2

5 6

D[9] D[10] + 1 2

10 11 9

slide-17
SLIDE 17

1 2 3

Example (4)

4 7 6 5 8

Q = {5} Q = {1, 6, 10} Q = {6 10 0 2} D[5] = 0 D[1] = D[5] + 1 = 1

9 10 11 12

Q = {6, 10, 0, 2} Q = {10, 0, 2, 11} Q = {0, 2, 11, 9} Q = {2 11 9 4} D[1] = D[5] + 1 = 1 D[6] = D[5] + 1 = 1 D[10] = D[5] + 1 = 1 D[0] = D[1] + 1 = 2

1 20

Q = {2, 11, 9, 4} Q = {11, 9, 4, 3} Q = {9, 4, 3, 12} Q = {4, 3, 12, 8}

2 3

D[0] = D[1] + 1 = 2 D[2] = D[1] + 1 = 2 D[11] = D[6] + 1 = 2 D[9] = D[10] + 1 = 2

5 6

Q {4, 3, 12, 8}

4

D[9] D[10] + 1 2 D[4] = D[0] + 1 = 3 D[3] = D[2] + 1 = 3 D[12] = D[11] + 1 = 3

8

D[12] D[11] 1 3 D[8] = D[9] + 1 = 3

10 11 9 12 77

slide-18
SLIDE 18

1 2 3

Example (5)

4 7 6 5 8

Q = {5} Q = {1, 6, 10} Q = {6 10 0 2} D[5] = 0 D[1] = D[5] + 1 = 1

9 10 11 12

Q = {6, 10, 0, 2} Q = {10, 0, 2, 11} Q = {0, 2, 11, 9} Q = {2 11 9 4} D[1] = D[5] + 1 = 1 D[6] = D[5] + 1 = 1 D[10] = D[5] + 1 = 1 D[0] = D[1] + 1 = 2

1 20

Q = {2, 11, 9, 4} Q = {11, 9, 4, 3} Q = {9, 4, 3, 12} Q = {4, 3, 12, 8}

2 3

D[0] = D[1] + 1 = 2 D[2] = D[1] + 1 = 2 D[11] = D[6] + 1 = 2 D[9] = D[10] + 1 = 2

5 6

Q {4, 3, 12, 8} Q = {3, 12, 8} Q = {12, 8, 7} Q = {8, 7}

4 7

D[9] D[10] + 1 2 D[4] = D[0] + 1 = 3 D[3] = D[2] + 1 = 3 D[12] = D[11] + 1 = 3 Q { , } Q = {7} Q = {}

8

D[12] D[11] 1 3 D[8] = D[9] + 1 = 3 D[7] = D[3] + 1 = 4

10 11 9 12

This is the BFS = SSSP  spanning tree when BFS is started from vertex 5