92
Graph Algorithms Peter Lammich 27. Januar 2020 1 / 31

Graph Algorithmssyllabus.cs.manchester.ac.uk/ugt/2019/COMP26120/2020slides/lecture12.pdfOutline 1 Directed Graphs Formal Definition Implementation 2 Graph Traversal Algorithms Generic

  • Upload
    others

  • View
    12

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Graph Algorithmssyllabus.cs.manchester.ac.uk/ugt/2019/COMP26120/2020slides/lecture12.pdfOutline 1 Directed Graphs Formal Definition Implementation 2 Graph Traversal Algorithms Generic

Graph Algorithms

Peter Lammich

27. Januar 2020

1 / 31

Page 2: Graph Algorithmssyllabus.cs.manchester.ac.uk/ugt/2019/COMP26120/2020slides/lecture12.pdfOutline 1 Directed Graphs Formal Definition Implementation 2 Graph Traversal Algorithms Generic

Outline

1 Directed GraphsFormal DefinitionImplementation

2 Graph Traversal AlgorithmsGeneric Graph TraversalDFS and BFSTopological SortingShortest Paths

3 Shortest Path in Weighted GraphsSingle-Source Shortest Path

2 / 31

Page 3: Graph Algorithmssyllabus.cs.manchester.ac.uk/ugt/2019/COMP26120/2020slides/lecture12.pdfOutline 1 Directed Graphs Formal Definition Implementation 2 Graph Traversal Algorithms Generic

Directed Graphs

• A directed graph is a set of nodes V and edges E ⊆ V × V

1

2

3

4

5

6

7

8

9

V = {1, 2, . . . , 9}E = {(1, 2), (1, 3), (2, 4), (2, 5), (3, 6),

(3, 7), (4, 8), (4, 9), (6, 5), (9, 2)}

3 / 31

Page 4: Graph Algorithmssyllabus.cs.manchester.ac.uk/ugt/2019/COMP26120/2020slides/lecture12.pdfOutline 1 Directed Graphs Formal Definition Implementation 2 Graph Traversal Algorithms Generic

Directed Graphs

• A directed graph is a set of nodes V and edges E ⊆ V × V

1

2

3

4

5

6

7

8

9

Path: Sequence of nodes u1 . . . un,with ∀i ∈ {1 . . . n − 1}. (ui , ui+1) ∈ Ee.g. 1, 2, 4, 8If not noted otherwise: paths are cycle-free, e.g.,no repeated nodes!

3 / 31

Page 5: Graph Algorithmssyllabus.cs.manchester.ac.uk/ugt/2019/COMP26120/2020slides/lecture12.pdfOutline 1 Directed Graphs Formal Definition Implementation 2 Graph Traversal Algorithms Generic

Directed Graphs

• A directed graph is a set of nodes V and edges E ⊆ V × V

1

2

3

4

5

6

7

8

9

Cycle: Path with same start and end nodee.g. 2, 4, 9, 2

3 / 31

Page 6: Graph Algorithmssyllabus.cs.manchester.ac.uk/ugt/2019/COMP26120/2020slides/lecture12.pdfOutline 1 Directed Graphs Formal Definition Implementation 2 Graph Traversal Algorithms Generic

Operations

• Here: nodes implicitly given by edges:V := {u | ∃v . (u, v) ∈ E ∨ (v , u) ∈ E}• empty returns empty graph. E ← ∅• addEdge(u,v) adds an edge. E ← E ∪ {(u, v)}• removeEdge(u,v) removes an edge. E ← E \ {(u, v)}• isEdge(u,v) checks for edge. (u, v) ∈ E• succs(u) returns successors of node. {v | (u, v) ∈ E}

4 / 31

Page 7: Graph Algorithmssyllabus.cs.manchester.ac.uk/ugt/2019/COMP26120/2020slides/lecture12.pdfOutline 1 Directed Graphs Formal Definition Implementation 2 Graph Traversal Algorithms Generic

Implementation

1

2

3

4

5

6

7

8

9

5 / 31

Page 8: Graph Algorithmssyllabus.cs.manchester.ac.uk/ugt/2019/COMP26120/2020slides/lecture12.pdfOutline 1 Directed Graphs Formal Definition Implementation 2 Graph Traversal Algorithms Generic

Implementation

1

2

3

4

5

6

7

8

9

Adjacency listStore list/set of successors for each nodesuccs[1] = {2, 3}succs[2] = {4, 5}succs[3] = {6, 7}succs[4] = {8, 9}succs[5] = {}succs[6] = {5}succs[7] = {}succs[8] = {}succs[9] = {2}

5 / 31

Page 9: Graph Algorithmssyllabus.cs.manchester.ac.uk/ugt/2019/COMP26120/2020slides/lecture12.pdfOutline 1 Directed Graphs Formal Definition Implementation 2 Graph Traversal Algorithms Generic

Implementation

1

2

3

4

5

6

7

8

9

Adjacency Matrixm(u, v) = T iff edge from u to v

1 2 3 4 5 6 7 8 91 F T T F F F F F F2 F F F T T F F F F3 F F F F F T T F F4 F F F F F F F T T5 F F F F F F F F F6 F F F F T F F F F7 F F F F F F F F F8 F F F F F F F F F9 F T F F F F F F F

5 / 31

Page 10: Graph Algorithmssyllabus.cs.manchester.ac.uk/ugt/2019/COMP26120/2020slides/lecture12.pdfOutline 1 Directed Graphs Formal Definition Implementation 2 Graph Traversal Algorithms Generic

Adjacency Lists

• Map each node to list of successors• E = {(u, v) | v ∈ succs(u)}• directly implements succs(u)• for integer nodes: use array of (dynamic) arrays

• Memory O(|V |+ |E |)

Operation Implementation ComplexityaddEdge(u, v) append v to succs(u) O(1) (amortized)removeEdge(u, v) remove v from succs(u) O(|V |)isEdge(u, v) search for v in succs(u) O(|V |)succs(u) return succs(u) O(1)

6 / 31

Page 11: Graph Algorithmssyllabus.cs.manchester.ac.uk/ugt/2019/COMP26120/2020slides/lecture12.pdfOutline 1 Directed Graphs Formal Definition Implementation 2 Graph Traversal Algorithms Generic

Adjacency Matrix

• Store |V | × |V | map to Booleans• E = {(u, v) | m(u, v) = true}• for integer nodes: use 2 dimensional array

• Memory O(|V |2).• Bad for sparse graphs (|E | << |V |2).

Operation Implementation ComplexityaddEdge(u, v) m(u, v)← true O(1)removeEdge(u, v) m(u, v)← false O(1)isEdge(u, v) return m(u, v) O(1)succs(u) return {v | m(u, v) = true} O(|V |)

7 / 31

Page 12: Graph Algorithmssyllabus.cs.manchester.ac.uk/ugt/2019/COMP26120/2020slides/lecture12.pdfOutline 1 Directed Graphs Formal Definition Implementation 2 Graph Traversal Algorithms Generic

Adjacency Matrix + Adjacency List

• Store graph simultaneously as adjacancy list and matrix• Memory: O(|V |2)• addEdge, isEdge, succs — O(1)• removeEdge — O(|V |)

8 / 31

Page 13: Graph Algorithmssyllabus.cs.manchester.ac.uk/ugt/2019/COMP26120/2020slides/lecture12.pdfOutline 1 Directed Graphs Formal Definition Implementation 2 Graph Traversal Algorithms Generic

Outline

1 Directed GraphsFormal DefinitionImplementation

2 Graph Traversal AlgorithmsGeneric Graph TraversalDFS and BFSTopological SortingShortest Paths

3 Shortest Path in Weighted GraphsSingle-Source Shortest Path

9 / 31

Page 14: Graph Algorithmssyllabus.cs.manchester.ac.uk/ugt/2019/COMP26120/2020slides/lecture12.pdfOutline 1 Directed Graphs Formal Definition Implementation 2 Graph Traversal Algorithms Generic

Generic Graph Traversal

• Explore edges to new nodes, until all nodes discovered

procedure explore(s)D ← {s}, F ← ∅while D 6= ∅ do

u ← Some u ∈ DD ← D \ {u}, F ← F ∪ {u}D ← D ∪ {v | (u, v) ∈ E ∧ v /∈ F}

return F

• F finished, outgoing edges have been explored• D discovered, but outgoing edges yet to be explored• explore(s) returns exactly the nodes reachable from s

10 / 31

Page 15: Graph Algorithmssyllabus.cs.manchester.ac.uk/ugt/2019/COMP26120/2020slides/lecture12.pdfOutline 1 Directed Graphs Formal Definition Implementation 2 Graph Traversal Algorithms Generic

Example

1

2

3

4

5

6

7

8

9

n discovered

n finished

D = { 1 }11 / 31

Page 16: Graph Algorithmssyllabus.cs.manchester.ac.uk/ugt/2019/COMP26120/2020slides/lecture12.pdfOutline 1 Directed Graphs Formal Definition Implementation 2 Graph Traversal Algorithms Generic

Example

1

2

3

4

5

6

7

8

9

n discovered

n finished

D = { 2 3 }11 / 31

Page 17: Graph Algorithmssyllabus.cs.manchester.ac.uk/ugt/2019/COMP26120/2020slides/lecture12.pdfOutline 1 Directed Graphs Formal Definition Implementation 2 Graph Traversal Algorithms Generic

Example

1

2

3

4

5

6

7

8

9

n discovered

n finished

D = { 3 4 5 }11 / 31

Page 18: Graph Algorithmssyllabus.cs.manchester.ac.uk/ugt/2019/COMP26120/2020slides/lecture12.pdfOutline 1 Directed Graphs Formal Definition Implementation 2 Graph Traversal Algorithms Generic

Example

1

2

3

4

5

6

7

8

9

n discovered

n finished

D = { 3 4 }11 / 31

Page 19: Graph Algorithmssyllabus.cs.manchester.ac.uk/ugt/2019/COMP26120/2020slides/lecture12.pdfOutline 1 Directed Graphs Formal Definition Implementation 2 Graph Traversal Algorithms Generic

Example

1

2

3

4

5

6

7

8

9

n discovered

n finished

D = { 4 6 7 }11 / 31

Page 20: Graph Algorithmssyllabus.cs.manchester.ac.uk/ugt/2019/COMP26120/2020slides/lecture12.pdfOutline 1 Directed Graphs Formal Definition Implementation 2 Graph Traversal Algorithms Generic

Example

1

2

3

4

5

6

7

8

9

n discovered

n finished

D = { 4 7 }11 / 31

Page 21: Graph Algorithmssyllabus.cs.manchester.ac.uk/ugt/2019/COMP26120/2020slides/lecture12.pdfOutline 1 Directed Graphs Formal Definition Implementation 2 Graph Traversal Algorithms Generic

Example

1

2

3

4

5

6

7

8

9

n discovered

n finished

D = { 7 8 9 }11 / 31

Page 22: Graph Algorithmssyllabus.cs.manchester.ac.uk/ugt/2019/COMP26120/2020slides/lecture12.pdfOutline 1 Directed Graphs Formal Definition Implementation 2 Graph Traversal Algorithms Generic

Example

1

2

3

4

5

6

7

8

9

n discovered

n finished

D = { 7 9 }11 / 31

Page 23: Graph Algorithmssyllabus.cs.manchester.ac.uk/ugt/2019/COMP26120/2020slides/lecture12.pdfOutline 1 Directed Graphs Formal Definition Implementation 2 Graph Traversal Algorithms Generic

Example

1

2

3

4

5

6

7

8

9

n discovered

n finished

D = { 9 }11 / 31

Page 24: Graph Algorithmssyllabus.cs.manchester.ac.uk/ugt/2019/COMP26120/2020slides/lecture12.pdfOutline 1 Directed Graphs Formal Definition Implementation 2 Graph Traversal Algorithms Generic

Example

1

2

3

4

5

6

7

8

9

n discovered

n finished

D = { }11 / 31

Page 25: Graph Algorithmssyllabus.cs.manchester.ac.uk/ugt/2019/COMP26120/2020slides/lecture12.pdfOutline 1 Directed Graphs Formal Definition Implementation 2 Graph Traversal Algorithms Generic

Generic Graph Traversal (Correctness)

explore(s) returns exactly the nodes reachable from s• Any node in D ∪ F is reachable:

• Initially, only s ∈ D ∪ F• only successors of nodes in D ∪ F added

• If a node is reachable, it will be included in F :• During the loop, successors of finished nodes are finished or discovered• Finally, D = ∅, thus successors of finished nodes are finished

=⇒ every reachable node is finished (follow path from s)

12 / 31

Page 26: Graph Algorithmssyllabus.cs.manchester.ac.uk/ugt/2019/COMP26120/2020slides/lecture12.pdfOutline 1 Directed Graphs Formal Definition Implementation 2 Graph Traversal Algorithms Generic

DFS and BFSprocedure explore(s)

D ← {s}, F ← ∅while D 6= ∅ do

u ← Some u ∈ DD ← D \ {u}, F ← F ∪ {u}D ← D ∪ {v | (u, v) ∈ E ∧ v /∈ F}

return F

• In which order do we process nodes from D• Last in / first out (stack): Depth First Search (DFS)• First in / first out (queue): Breadth First Search (BFS)

13 / 31

Page 27: Graph Algorithmssyllabus.cs.manchester.ac.uk/ugt/2019/COMP26120/2020slides/lecture12.pdfOutline 1 Directed Graphs Formal Definition Implementation 2 Graph Traversal Algorithms Generic

Example: DFS

1

2

3

4

5

6

7

8

9

n discovered

n finished

D (LiFo): [ 1 ]14 / 31

Page 28: Graph Algorithmssyllabus.cs.manchester.ac.uk/ugt/2019/COMP26120/2020slides/lecture12.pdfOutline 1 Directed Graphs Formal Definition Implementation 2 Graph Traversal Algorithms Generic

Example: DFS

1

2

3

4

5

6

7

8

9

n discovered

n finished

D (LiFo): [ 3 2 ]14 / 31

Page 29: Graph Algorithmssyllabus.cs.manchester.ac.uk/ugt/2019/COMP26120/2020slides/lecture12.pdfOutline 1 Directed Graphs Formal Definition Implementation 2 Graph Traversal Algorithms Generic

Example: DFS

1

2

3

4

5

6

7

8

9

n discovered

n finished

D (LiFo): [ 3 5 4 ]14 / 31

Page 30: Graph Algorithmssyllabus.cs.manchester.ac.uk/ugt/2019/COMP26120/2020slides/lecture12.pdfOutline 1 Directed Graphs Formal Definition Implementation 2 Graph Traversal Algorithms Generic

Example: DFS

1

2

3

4

5

6

7

8

9

n discovered

n finished

D (LiFo): [ 3 5 8 9 ]14 / 31

Page 31: Graph Algorithmssyllabus.cs.manchester.ac.uk/ugt/2019/COMP26120/2020slides/lecture12.pdfOutline 1 Directed Graphs Formal Definition Implementation 2 Graph Traversal Algorithms Generic

Example: DFS

1

2

3

4

5

6

7

8

9

n discovered

n finished

D (LiFo): [ 3 5 8 ]14 / 31

Page 32: Graph Algorithmssyllabus.cs.manchester.ac.uk/ugt/2019/COMP26120/2020slides/lecture12.pdfOutline 1 Directed Graphs Formal Definition Implementation 2 Graph Traversal Algorithms Generic

Example: DFS

1

2

3

4

5

6

7

8

9

n discovered

n finished

D (LiFo): [ 3 5 ]14 / 31

Page 33: Graph Algorithmssyllabus.cs.manchester.ac.uk/ugt/2019/COMP26120/2020slides/lecture12.pdfOutline 1 Directed Graphs Formal Definition Implementation 2 Graph Traversal Algorithms Generic

Example: DFS

1

2

3

4

5

6

7

8

9

n discovered

n finished

D (LiFo): [ 3 ]14 / 31

Page 34: Graph Algorithmssyllabus.cs.manchester.ac.uk/ugt/2019/COMP26120/2020slides/lecture12.pdfOutline 1 Directed Graphs Formal Definition Implementation 2 Graph Traversal Algorithms Generic

Example: DFS

1

2

3

4

5

6

7

8

9

n discovered

n finished

D (LiFo): [ 6 7 ]14 / 31

Page 35: Graph Algorithmssyllabus.cs.manchester.ac.uk/ugt/2019/COMP26120/2020slides/lecture12.pdfOutline 1 Directed Graphs Formal Definition Implementation 2 Graph Traversal Algorithms Generic

Example: DFS

1

2

3

4

5

6

7

8

9

n discovered

n finished

D (LiFo): [ 6 ]14 / 31

Page 36: Graph Algorithmssyllabus.cs.manchester.ac.uk/ugt/2019/COMP26120/2020slides/lecture12.pdfOutline 1 Directed Graphs Formal Definition Implementation 2 Graph Traversal Algorithms Generic

Example: DFS

1

2

3

4

5

6

7

8

9

n discovered

n finished

D (LiFo): [ ]14 / 31

Page 37: Graph Algorithmssyllabus.cs.manchester.ac.uk/ugt/2019/COMP26120/2020slides/lecture12.pdfOutline 1 Directed Graphs Formal Definition Implementation 2 Graph Traversal Algorithms Generic

Example: BFS

1

2

3

4

5

6

7

8

9

n discovered

n finished

D (FiFo): [ 1

2 3 4 5 7 6 9 8

]15 / 31

Page 38: Graph Algorithmssyllabus.cs.manchester.ac.uk/ugt/2019/COMP26120/2020slides/lecture12.pdfOutline 1 Directed Graphs Formal Definition Implementation 2 Graph Traversal Algorithms Generic

Example: BFS

1

2

3

4

5

6

7

8

9

n discovered

n finished

D (FiFo): [

1

2 3

4 5 7 6 9 8

]15 / 31

Page 39: Graph Algorithmssyllabus.cs.manchester.ac.uk/ugt/2019/COMP26120/2020slides/lecture12.pdfOutline 1 Directed Graphs Formal Definition Implementation 2 Graph Traversal Algorithms Generic

Example: BFS

1

2

3

4

5

6

7

8

9

n discovered

n finished

D (FiFo): [

1 2

3 4 5

7 6 9 8

]15 / 31

Page 40: Graph Algorithmssyllabus.cs.manchester.ac.uk/ugt/2019/COMP26120/2020slides/lecture12.pdfOutline 1 Directed Graphs Formal Definition Implementation 2 Graph Traversal Algorithms Generic

Example: BFS

1

2

3

4

5

6

7

8

9

n discovered

n finished

D (FiFo): [

1 2 3

4 5 7 6

9 8

]15 / 31

Page 41: Graph Algorithmssyllabus.cs.manchester.ac.uk/ugt/2019/COMP26120/2020slides/lecture12.pdfOutline 1 Directed Graphs Formal Definition Implementation 2 Graph Traversal Algorithms Generic

Example: BFS

1

2

3

4

5

6

7

8

9

n discovered

n finished

D (FiFo): [

1 2 3 4

5 7 6 9 8 ]15 / 31

Page 42: Graph Algorithmssyllabus.cs.manchester.ac.uk/ugt/2019/COMP26120/2020slides/lecture12.pdfOutline 1 Directed Graphs Formal Definition Implementation 2 Graph Traversal Algorithms Generic

Example: BFS

1

2

3

4

5

6

7

8

9

n discovered

n finished

D (FiFo): [

1 2 3 4 5

7 6 9 8 ]15 / 31

Page 43: Graph Algorithmssyllabus.cs.manchester.ac.uk/ugt/2019/COMP26120/2020slides/lecture12.pdfOutline 1 Directed Graphs Formal Definition Implementation 2 Graph Traversal Algorithms Generic

Example: BFS

1

2

3

4

5

6

7

8

9

n discovered

n finished

D (FiFo): [

1 2 3 4 5 7

6 9 8 ]15 / 31

Page 44: Graph Algorithmssyllabus.cs.manchester.ac.uk/ugt/2019/COMP26120/2020slides/lecture12.pdfOutline 1 Directed Graphs Formal Definition Implementation 2 Graph Traversal Algorithms Generic

Example: BFS

1

2

3

4

5

6

7

8

9

n discovered

n finished

D (FiFo): [

1 2 3 4 5 7 6

9 8 ]15 / 31

Page 45: Graph Algorithmssyllabus.cs.manchester.ac.uk/ugt/2019/COMP26120/2020slides/lecture12.pdfOutline 1 Directed Graphs Formal Definition Implementation 2 Graph Traversal Algorithms Generic

Example: BFS

1

2

3

4

5

6

7

8

9

n discovered

n finished

D (FiFo): [

1 2 3 4 5 7 6 9

8 ]15 / 31

Page 46: Graph Algorithmssyllabus.cs.manchester.ac.uk/ugt/2019/COMP26120/2020slides/lecture12.pdfOutline 1 Directed Graphs Formal Definition Implementation 2 Graph Traversal Algorithms Generic

Example: BFS

1

2

3

4

5

6

7

8

9

n discovered

n finished

D (FiFo): [

1 2 3 4 5 7 6 9 8

]15 / 31

Page 47: Graph Algorithmssyllabus.cs.manchester.ac.uk/ugt/2019/COMP26120/2020slides/lecture12.pdfOutline 1 Directed Graphs Formal Definition Implementation 2 Graph Traversal Algorithms Generic

Recursive DFS

• DFS has nice recursive implementation

procedure dfsRec(F , u)if u /∈ F then

F ← F ∪ {u}for all v with (u, v) ∈ E do

F ← dfsRec(F,v)return F

procedure dfs(s) return dfsRec(∅, s)

16 / 31

Page 48: Graph Algorithmssyllabus.cs.manchester.ac.uk/ugt/2019/COMP26120/2020slides/lecture12.pdfOutline 1 Directed Graphs Formal Definition Implementation 2 Graph Traversal Algorithms Generic

Topological Sorting

• Set of tasks (e.g. build jobs in Makefile)• Dependencies, i.e. tasks that needs to be completed before a task can

be started• Model as directed graph. Edge (u, v): v depends on u.• Find a build sequence

17 / 31

Page 49: Graph Algorithmssyllabus.cs.manchester.ac.uk/ugt/2019/COMP26120/2020slides/lecture12.pdfOutline 1 Directed Graphs Formal Definition Implementation 2 Graph Traversal Algorithms Generic

Examplemain: main.o hashtable.o dynarray.o graph.o

gcc ...

main.o: main.chashtable.o: hashtable.h hashtable.c dynarray.hdynarray.o: dynarray.h dynarray.cgraph.o: graph.h graph.c

main

main.o

hashtable.o

dynarray.o

graph.o

main.c

hashtable.h

hashtable.c

dynarray.h

dynarray.c

graph.h

graph.c

Possible build sequence:graph.o, dynarray.o, hashtable.o, main.o,main

18 / 31

Page 50: Graph Algorithmssyllabus.cs.manchester.ac.uk/ugt/2019/COMP26120/2020slides/lecture12.pdfOutline 1 Directed Graphs Formal Definition Implementation 2 Graph Traversal Algorithms Generic

Topological Sorting

• Arrange nodes sequentially, such that all edges point forwards• Intuition: All prerequisites come earlier in sequence

• Topological sorting possible iff graph has no cycles• Directed Acyclic Graph (DAG)

• Now: DFS based algorithm for topological sorting and cycle detection

19 / 31

Page 51: Graph Algorithmssyllabus.cs.manchester.ac.uk/ugt/2019/COMP26120/2020slides/lecture12.pdfOutline 1 Directed Graphs Formal Definition Implementation 2 Graph Traversal Algorithms Generic

Cycle detection with DFS

• When first encountering a node, mark it as open.• Only when finished exploring its children, mark it as done• Whenever we encounter an open node again, it’s a cycle

procedure dfsRec(F , u)if F (u) = N then

F (u)← O // Start processing nodefor all v with (u, v) ∈ E do

F ← dfsRec(F,v)F (u)← D // Done processing node

else if F (u) = O thenError: found cycle

return Fprocedure dfs(s)

F (u)← N for all nodes u return dfsRec(F , s)

20 / 31

Page 52: Graph Algorithmssyllabus.cs.manchester.ac.uk/ugt/2019/COMP26120/2020slides/lecture12.pdfOutline 1 Directed Graphs Formal Definition Implementation 2 Graph Traversal Algorithms Generic

Topological Sorting

procedure dfsRec(F , u)if F (u) = N then

F (u)← O // Start processing nodefor all v with (u, v) ∈ E do

F ← dfsRec(F,v)F (u)← D // Done processing node

else if F (u) = O thenError: found cycle

return Fprocedure dfs(s)

F (u)← N for all nodes u return dfsRec(F , s)

• When node is done, all its successors are already done=⇒ Nodes done in reverse topological order• to get topological sort: prepend nodes to list when marked as done

21 / 31

Page 53: Graph Algorithmssyllabus.cs.manchester.ac.uk/ugt/2019/COMP26120/2020slides/lecture12.pdfOutline 1 Directed Graphs Formal Definition Implementation 2 Graph Traversal Algorithms Generic

Example

1

2

3

4

56

n New

n Open

n Done

Result List:

1 3 5 2 6 4

22 / 31

Page 54: Graph Algorithmssyllabus.cs.manchester.ac.uk/ugt/2019/COMP26120/2020slides/lecture12.pdfOutline 1 Directed Graphs Formal Definition Implementation 2 Graph Traversal Algorithms Generic

Example

1

2

3

4

56

n New

n Open

n Done

Result List:

1 3 5 2 6 4

22 / 31

Page 55: Graph Algorithmssyllabus.cs.manchester.ac.uk/ugt/2019/COMP26120/2020slides/lecture12.pdfOutline 1 Directed Graphs Formal Definition Implementation 2 Graph Traversal Algorithms Generic

Example

1

2

3

4

56

n New

n Open

n Done

Result List:

1 3 5 2 6 4

22 / 31

Page 56: Graph Algorithmssyllabus.cs.manchester.ac.uk/ugt/2019/COMP26120/2020slides/lecture12.pdfOutline 1 Directed Graphs Formal Definition Implementation 2 Graph Traversal Algorithms Generic

Example

1

2

3

4

56

n New

n Open

n Done

Result List:

1 3 5 2 6 4

22 / 31

Page 57: Graph Algorithmssyllabus.cs.manchester.ac.uk/ugt/2019/COMP26120/2020slides/lecture12.pdfOutline 1 Directed Graphs Formal Definition Implementation 2 Graph Traversal Algorithms Generic

Example

1

2

3

4

56

n New

n Open

n Done

Result List:

1 3 5 2 6

4

22 / 31

Page 58: Graph Algorithmssyllabus.cs.manchester.ac.uk/ugt/2019/COMP26120/2020slides/lecture12.pdfOutline 1 Directed Graphs Formal Definition Implementation 2 Graph Traversal Algorithms Generic

Example

1

2

3

4

56

n New

n Open

n Done

Result List:

1 3 5 2 6

4

22 / 31

Page 59: Graph Algorithmssyllabus.cs.manchester.ac.uk/ugt/2019/COMP26120/2020slides/lecture12.pdfOutline 1 Directed Graphs Formal Definition Implementation 2 Graph Traversal Algorithms Generic

Example

1

2

3

4

56

n New

n Open

n Done

Result List:

1 3 5 2

6 4

22 / 31

Page 60: Graph Algorithmssyllabus.cs.manchester.ac.uk/ugt/2019/COMP26120/2020slides/lecture12.pdfOutline 1 Directed Graphs Formal Definition Implementation 2 Graph Traversal Algorithms Generic

Example

1

2

3

4

56

n New

n Open

n Done

Result List:

1 3 5

2 6 4

22 / 31

Page 61: Graph Algorithmssyllabus.cs.manchester.ac.uk/ugt/2019/COMP26120/2020slides/lecture12.pdfOutline 1 Directed Graphs Formal Definition Implementation 2 Graph Traversal Algorithms Generic

Example

1

2

3

4

56

n New

n Open

n Done

Result List:

1 3 5

2 6 4

22 / 31

Page 62: Graph Algorithmssyllabus.cs.manchester.ac.uk/ugt/2019/COMP26120/2020slides/lecture12.pdfOutline 1 Directed Graphs Formal Definition Implementation 2 Graph Traversal Algorithms Generic

Example

1

2

3

4

56

n New

n Open

n Done

Result List:

1 3 5

2 6 4

22 / 31

Page 63: Graph Algorithmssyllabus.cs.manchester.ac.uk/ugt/2019/COMP26120/2020slides/lecture12.pdfOutline 1 Directed Graphs Formal Definition Implementation 2 Graph Traversal Algorithms Generic

Example

1

2

3

4

56

n New

n Open

n Done

Result List:

1 3

5 2 6 4

22 / 31

Page 64: Graph Algorithmssyllabus.cs.manchester.ac.uk/ugt/2019/COMP26120/2020slides/lecture12.pdfOutline 1 Directed Graphs Formal Definition Implementation 2 Graph Traversal Algorithms Generic

Example

1

2

3

4

56

n New

n Open

n Done

Result List:

1

3 5 2 6 4

22 / 31

Page 65: Graph Algorithmssyllabus.cs.manchester.ac.uk/ugt/2019/COMP26120/2020slides/lecture12.pdfOutline 1 Directed Graphs Formal Definition Implementation 2 Graph Traversal Algorithms Generic

Example

1

2

3

4

56

n New

n Open

n Done

Result List: 1 3 5 2 6 4

22 / 31

Page 66: Graph Algorithmssyllabus.cs.manchester.ac.uk/ugt/2019/COMP26120/2020slides/lecture12.pdfOutline 1 Directed Graphs Formal Definition Implementation 2 Graph Traversal Algorithms Generic

Example

1

2

3

4

56

n New

n Open

n Done

Result List:

4Error: found cycle

23 / 31

Page 67: Graph Algorithmssyllabus.cs.manchester.ac.uk/ugt/2019/COMP26120/2020slides/lecture12.pdfOutline 1 Directed Graphs Formal Definition Implementation 2 Graph Traversal Algorithms Generic

Example

1

2

3

4

56

n New

n Open

n Done

Result List:

4Error: found cycle

23 / 31

Page 68: Graph Algorithmssyllabus.cs.manchester.ac.uk/ugt/2019/COMP26120/2020slides/lecture12.pdfOutline 1 Directed Graphs Formal Definition Implementation 2 Graph Traversal Algorithms Generic

Example

1

2

3

4

56

n New

n Open

n Done

Result List:

4Error: found cycle

23 / 31

Page 69: Graph Algorithmssyllabus.cs.manchester.ac.uk/ugt/2019/COMP26120/2020slides/lecture12.pdfOutline 1 Directed Graphs Formal Definition Implementation 2 Graph Traversal Algorithms Generic

Example

1

2

3

4

56

n New

n Open

n Done

Result List: 4

Error: found cycle

23 / 31

Page 70: Graph Algorithmssyllabus.cs.manchester.ac.uk/ugt/2019/COMP26120/2020slides/lecture12.pdfOutline 1 Directed Graphs Formal Definition Implementation 2 Graph Traversal Algorithms Generic

Example

1

2

3

4

56

n New

n Open

n Done

Result List: 4

Error: found cycle

23 / 31

Page 71: Graph Algorithmssyllabus.cs.manchester.ac.uk/ugt/2019/COMP26120/2020slides/lecture12.pdfOutline 1 Directed Graphs Formal Definition Implementation 2 Graph Traversal Algorithms Generic

Example

1

2

3

4

56

n New

n Open

n Done

Result List: 4

Error: found cycle

23 / 31

Page 72: Graph Algorithmssyllabus.cs.manchester.ac.uk/ugt/2019/COMP26120/2020slides/lecture12.pdfOutline 1 Directed Graphs Formal Definition Implementation 2 Graph Traversal Algorithms Generic

Example

1

2

3

4

56

n New

n Open

n Done

Result List: 4

Error: found cycle

23 / 31

Page 73: Graph Algorithmssyllabus.cs.manchester.ac.uk/ugt/2019/COMP26120/2020slides/lecture12.pdfOutline 1 Directed Graphs Formal Definition Implementation 2 Graph Traversal Algorithms Generic

Example

1

2

3

4

56

n New

n Open

n Done

Result List: 4

Error: found cycle

23 / 31

Page 74: Graph Algorithmssyllabus.cs.manchester.ac.uk/ugt/2019/COMP26120/2020slides/lecture12.pdfOutline 1 Directed Graphs Formal Definition Implementation 2 Graph Traversal Algorithms Generic

Example

1

2

3

4

56

n New

n Open

n Done

Result List: 4Error: found cycle

23 / 31

Page 75: Graph Algorithmssyllabus.cs.manchester.ac.uk/ugt/2019/COMP26120/2020slides/lecture12.pdfOutline 1 Directed Graphs Formal Definition Implementation 2 Graph Traversal Algorithms Generic

Shortest paths with BFS

• Shortest path from s to some node?• shortest = minimal number of edges

• BFS visits nodes in order of their distance from s!• Obtain path via predecessor map:

• For each node, store node from which it was discovered• Follow these nodes backwards, until s is reached• Convention: predecessor of s is s itself.

24 / 31

Page 76: Graph Algorithmssyllabus.cs.manchester.ac.uk/ugt/2019/COMP26120/2020slides/lecture12.pdfOutline 1 Directed Graphs Formal Definition Implementation 2 Graph Traversal Algorithms Generic

BFS Shortest Path

procedure shortestPaths(s)D ← [s], F ← ∅, π(s)← swhile D 6= [] do

(u, D)← dequeue(D)D ← D \ {u}, F ← F ∪ {u}for all v with (u, v) ∈ E ∧ v /∈ F do

D ← enqueue(v), π(v)← ureturn π

procedure getPath(π, u)p ← [u]while π(u) 6= u do

u ← π(u), p ← up

25 / 31

Page 77: Graph Algorithmssyllabus.cs.manchester.ac.uk/ugt/2019/COMP26120/2020slides/lecture12.pdfOutline 1 Directed Graphs Formal Definition Implementation 2 Graph Traversal Algorithms Generic

Example: BFS shortest paths

1

2

3

4

5

6

7

8

9

n discovered

n finished

predecessor map

D (FiFo): [ 1 |

2 3 | 4 5 7 6 | 9 8

]26 / 31

Page 78: Graph Algorithmssyllabus.cs.manchester.ac.uk/ugt/2019/COMP26120/2020slides/lecture12.pdfOutline 1 Directed Graphs Formal Definition Implementation 2 Graph Traversal Algorithms Generic

Example: BFS shortest paths

1

2

3

4

5

6

7

8

9

n discovered

n finished

predecessor map

D (FiFo): [ 1 | 2 3 |

4 5 7 6 | 9 8

]26 / 31

Page 79: Graph Algorithmssyllabus.cs.manchester.ac.uk/ugt/2019/COMP26120/2020slides/lecture12.pdfOutline 1 Directed Graphs Formal Definition Implementation 2 Graph Traversal Algorithms Generic

Example: BFS shortest paths

1

2

3

4

5

6

7

8

9

n discovered

n finished

predecessor map

D (FiFo): [ 1 | 2 3 | 4 5

7 6 | 9 8

]26 / 31

Page 80: Graph Algorithmssyllabus.cs.manchester.ac.uk/ugt/2019/COMP26120/2020slides/lecture12.pdfOutline 1 Directed Graphs Formal Definition Implementation 2 Graph Traversal Algorithms Generic

Example: BFS shortest paths

1

2

3

4

5

6

7

8

9

n discovered

n finished

predecessor map

D (FiFo): [ 1 | 2 3 | 4 5 7 6 |

9 8

]26 / 31

Page 81: Graph Algorithmssyllabus.cs.manchester.ac.uk/ugt/2019/COMP26120/2020slides/lecture12.pdfOutline 1 Directed Graphs Formal Definition Implementation 2 Graph Traversal Algorithms Generic

Example: BFS shortest paths

1

2

3

4

5

6

7

8

9

n discovered

n finished

predecessor map

D (FiFo): [ 1 | 2 3 | 4 5 7 6 | 9 8 ]26 / 31

Page 82: Graph Algorithmssyllabus.cs.manchester.ac.uk/ugt/2019/COMP26120/2020slides/lecture12.pdfOutline 1 Directed Graphs Formal Definition Implementation 2 Graph Traversal Algorithms Generic

Example: BFS shortest paths

1

2

3

4

5

6

7

8

9

n discovered

n finished

predecessor map

D (FiFo): [ 1 | 2 3 | 4 5 7 6 | 9 8 ]26 / 31

Page 83: Graph Algorithmssyllabus.cs.manchester.ac.uk/ugt/2019/COMP26120/2020slides/lecture12.pdfOutline 1 Directed Graphs Formal Definition Implementation 2 Graph Traversal Algorithms Generic

Example: BFS shortest paths

1

2

3

4

5

6

7

8

9

n discovered

n finished

predecessor map

D (FiFo): [ 1 | 2 3 | 4 5 7 6 | 9 8 ]26 / 31

Page 84: Graph Algorithmssyllabus.cs.manchester.ac.uk/ugt/2019/COMP26120/2020slides/lecture12.pdfOutline 1 Directed Graphs Formal Definition Implementation 2 Graph Traversal Algorithms Generic

Example: BFS shortest paths

1

2

3

4

5

6

7

8

9

n discovered

n finished

predecessor map

D (FiFo): [ 1 | 2 3 | 4 5 7 6 | 9 8 ]26 / 31

Page 85: Graph Algorithmssyllabus.cs.manchester.ac.uk/ugt/2019/COMP26120/2020slides/lecture12.pdfOutline 1 Directed Graphs Formal Definition Implementation 2 Graph Traversal Algorithms Generic

Example: BFS shortest paths

1

2

3

4

5

6

7

8

9

n discovered

n finished

predecessor map

D (FiFo): [ 1 | 2 3 | 4 5 7 6 | 9 8 ]26 / 31

Page 86: Graph Algorithmssyllabus.cs.manchester.ac.uk/ugt/2019/COMP26120/2020slides/lecture12.pdfOutline 1 Directed Graphs Formal Definition Implementation 2 Graph Traversal Algorithms Generic

Example: BFS shortest paths

1

2

3

4

5

6

7

8

9

n discovered

n finished

predecessor map

D (FiFo): [ 1 | 2 3 | 4 5 7 6 | 9 8 ]26 / 31

Page 87: Graph Algorithmssyllabus.cs.manchester.ac.uk/ugt/2019/COMP26120/2020slides/lecture12.pdfOutline 1 Directed Graphs Formal Definition Implementation 2 Graph Traversal Algorithms Generic

Example: BFS shortest paths

1

2

3

4

5

6

7

8

9

n discovered

n finished

predecessor map

D (FiFo): [ 1 | 2 3 | 4 5 7 6 | 9 8 ]26 / 31

Page 88: Graph Algorithmssyllabus.cs.manchester.ac.uk/ugt/2019/COMP26120/2020slides/lecture12.pdfOutline 1 Directed Graphs Formal Definition Implementation 2 Graph Traversal Algorithms Generic

Example: BFS shortest paths

1

2

3

4

5

6

7

8

9

n discovered

n finished

predecessor map

getPath(8): p = [

1 2 4

8 ]26 / 31

Page 89: Graph Algorithmssyllabus.cs.manchester.ac.uk/ugt/2019/COMP26120/2020slides/lecture12.pdfOutline 1 Directed Graphs Formal Definition Implementation 2 Graph Traversal Algorithms Generic

Example: BFS shortest paths

1

2

3

4

5

6

7

8

9

n discovered

n finished

predecessor map

getPath(8): p = [

1 2

4 8 ]26 / 31

Page 90: Graph Algorithmssyllabus.cs.manchester.ac.uk/ugt/2019/COMP26120/2020slides/lecture12.pdfOutline 1 Directed Graphs Formal Definition Implementation 2 Graph Traversal Algorithms Generic

Example: BFS shortest paths

1

2

3

4

5

6

7

8

9

n discovered

n finished

predecessor map

getPath(8): p = [

1

2 4 8 ]26 / 31

Page 91: Graph Algorithmssyllabus.cs.manchester.ac.uk/ugt/2019/COMP26120/2020slides/lecture12.pdfOutline 1 Directed Graphs Formal Definition Implementation 2 Graph Traversal Algorithms Generic

Example: BFS shortest paths

1

2

3

4

5

6

7

8

9

n discovered

n finished

predecessor map

getPath(8): p = [ 1 2 4 8 ]26 / 31

Page 92: Graph Algorithmssyllabus.cs.manchester.ac.uk/ugt/2019/COMP26120/2020slides/lecture12.pdfOutline 1 Directed Graphs Formal Definition Implementation 2 Graph Traversal Algorithms Generic

Outline

1 Directed GraphsFormal DefinitionImplementation

2 Graph Traversal AlgorithmsGeneric Graph TraversalDFS and BFSTopological SortingShortest Paths

3 Shortest Path in Weighted GraphsSingle-Source Shortest Path

27 / 31