graph algorithmssyllabus.cs.manchester.ac.uk/ugt/2019/comp26120/2020slides/lecture12.pdfoutline 1...
Post on 22-May-2020
14 Views
Preview:
TRANSCRIPT
Graph Algorithms
Peter Lammich
27. Januar 2020
1 / 31
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
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
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
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
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
Implementation
1
2
3
4
5
6
7
8
9
5 / 31
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
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
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
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
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
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
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
Example
1
2
3
4
5
6
7
8
9
n discovered
n finished
D = { 1 }11 / 31
Example
1
2
3
4
5
6
7
8
9
n discovered
n finished
D = { 2 3 }11 / 31
Example
1
2
3
4
5
6
7
8
9
n discovered
n finished
D = { 3 4 5 }11 / 31
Example
1
2
3
4
5
6
7
8
9
n discovered
n finished
D = { 3 4 }11 / 31
Example
1
2
3
4
5
6
7
8
9
n discovered
n finished
D = { 4 6 7 }11 / 31
Example
1
2
3
4
5
6
7
8
9
n discovered
n finished
D = { 4 7 }11 / 31
Example
1
2
3
4
5
6
7
8
9
n discovered
n finished
D = { 7 8 9 }11 / 31
Example
1
2
3
4
5
6
7
8
9
n discovered
n finished
D = { 7 9 }11 / 31
Example
1
2
3
4
5
6
7
8
9
n discovered
n finished
D = { 9 }11 / 31
Example
1
2
3
4
5
6
7
8
9
n discovered
n finished
D = { }11 / 31
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
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
Example: DFS
1
2
3
4
5
6
7
8
9
n discovered
n finished
D (LiFo): [ 1 ]14 / 31
Example: DFS
1
2
3
4
5
6
7
8
9
n discovered
n finished
D (LiFo): [ 3 2 ]14 / 31
Example: DFS
1
2
3
4
5
6
7
8
9
n discovered
n finished
D (LiFo): [ 3 5 4 ]14 / 31
Example: DFS
1
2
3
4
5
6
7
8
9
n discovered
n finished
D (LiFo): [ 3 5 8 9 ]14 / 31
Example: DFS
1
2
3
4
5
6
7
8
9
n discovered
n finished
D (LiFo): [ 3 5 8 ]14 / 31
Example: DFS
1
2
3
4
5
6
7
8
9
n discovered
n finished
D (LiFo): [ 3 5 ]14 / 31
Example: DFS
1
2
3
4
5
6
7
8
9
n discovered
n finished
D (LiFo): [ 3 ]14 / 31
Example: DFS
1
2
3
4
5
6
7
8
9
n discovered
n finished
D (LiFo): [ 6 7 ]14 / 31
Example: DFS
1
2
3
4
5
6
7
8
9
n discovered
n finished
D (LiFo): [ 6 ]14 / 31
Example: DFS
1
2
3
4
5
6
7
8
9
n discovered
n finished
D (LiFo): [ ]14 / 31
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
Example
1
2
3
4
56
n New
n Open
n Done
Result List:
1 3 5 2 6 4
22 / 31
Example
1
2
3
4
56
n New
n Open
n Done
Result List:
1 3 5 2 6 4
22 / 31
Example
1
2
3
4
56
n New
n Open
n Done
Result List:
1 3 5 2 6 4
22 / 31
Example
1
2
3
4
56
n New
n Open
n Done
Result List:
1 3 5 2 6 4
22 / 31
Example
1
2
3
4
56
n New
n Open
n Done
Result List:
1 3 5 2 6
4
22 / 31
Example
1
2
3
4
56
n New
n Open
n Done
Result List:
1 3 5 2 6
4
22 / 31
Example
1
2
3
4
56
n New
n Open
n Done
Result List:
1 3 5 2
6 4
22 / 31
Example
1
2
3
4
56
n New
n Open
n Done
Result List:
1 3 5
2 6 4
22 / 31
Example
1
2
3
4
56
n New
n Open
n Done
Result List:
1 3 5
2 6 4
22 / 31
Example
1
2
3
4
56
n New
n Open
n Done
Result List:
1 3 5
2 6 4
22 / 31
Example
1
2
3
4
56
n New
n Open
n Done
Result List:
1 3
5 2 6 4
22 / 31
Example
1
2
3
4
56
n New
n Open
n Done
Result List:
1
3 5 2 6 4
22 / 31
Example
1
2
3
4
56
n New
n Open
n Done
Result List: 1 3 5 2 6 4
22 / 31
Example
1
2
3
4
56
n New
n Open
n Done
Result List:
4Error: found cycle
23 / 31
Example
1
2
3
4
56
n New
n Open
n Done
Result List:
4Error: found cycle
23 / 31
Example
1
2
3
4
56
n New
n Open
n Done
Result List:
4Error: found cycle
23 / 31
Example
1
2
3
4
56
n New
n Open
n Done
Result List: 4
Error: found cycle
23 / 31
Example
1
2
3
4
56
n New
n Open
n Done
Result List: 4
Error: found cycle
23 / 31
Example
1
2
3
4
56
n New
n Open
n Done
Result List: 4
Error: found cycle
23 / 31
Example
1
2
3
4
56
n New
n Open
n Done
Result List: 4
Error: found cycle
23 / 31
Example
1
2
3
4
56
n New
n Open
n Done
Result List: 4
Error: found cycle
23 / 31
Example
1
2
3
4
56
n New
n Open
n Done
Result List: 4Error: found cycle
23 / 31
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
top related