Graph Implementations
Designing traversal algorithms and augmenting graphs to solve pathing problems.
Kevin Lin, with thanks to many others.
1
Shortest Paths in a Directed Graph
Run BFS from 7 and give the values of the resulting edgeTo map.
edgeTo[0] =
edgeTo[1] =
edgeTo[2] =
edgeTo[3] =
edgeTo[4] =
edgeTo[5] =
edgeTo[6] =
edgeTo[7] =
edgeTo[8] =
edgeTo[9] =
2
Q
Algorithms (Robert Sedgewick, Kevin Wayne/Princeton)
5
0
1
4
6
3
2
7
8
9
Q1: Run BFS from 7 and give the values of the resulting edgeTo map.
edgeTo[0] =
edgeTo[1] =
edgeTo[2] =
edgeTo[3] =
edgeTo[4] =
edgeTo[5] =
edgeTo[6] =
edgeTo[7] =
edgeTo[8] =
edgeTo[9] =
Shortest Paths in a Directed Graph
Run BFS from 7 and give the values of the resulting edgeTo map.
edgeTo[0] =
edgeTo[1] =
edgeTo[2] =
edgeTo[3] =
edgeTo[4] =
edgeTo[5] =
edgeTo[6] =
edgeTo[7] =
edgeTo[8] =
edgeTo[9] =
3
A
Algorithms (Robert Sedgewick, Kevin Wayne/Princeton)
5
0
1
4
6
3
2
7
8
9
Shortest Directed Cycle
Given a digraph, design an algorithm to find a directed cycle with the minimum number of edges (or report that the graph is acyclic) in O(EV) time.
4
Q
Algorithms (Robert Sedgewick, Kevin Wayne/Princeton)
0
1
4
6
7
8
9
5
3
2
?: What is the runtime for graph traversal given an adjacency list representation?
?: How many graph traversals can be completed in O(EV) time?
Q1: Given a digraph, design an algorithm to find a directed cycle with the minimum number of edges (or report that the graph is acyclic) in O(EV) time.
Shortest Directed Cycle
Given a digraph, design an algorithm to find a directed cycle with the minimum number of edges (or report that the graph is acyclic) in O(EV) time.
5
A
Algorithms (Robert Sedgewick, Kevin Wayne/Princeton)
0
1
4
6
7
8
9
5
3
2
Multiple-Source Shortest Paths
Given a digraph and a set of source vertices, find shortest path from any vertex in the set to every other vertex.
S = {1, 5, 7}.
Shortest path to 0 is 7-6-0.
Shortest path to 2 is 5-2.
Shortest path to 4 is 5-2-4.
6
Q
Algorithms (Robert Sedgewick, Kevin Wayne/Princeton)
5
0
1
4
6
3
2
7
8
9
?: Give a runtime bound for this problem.
Q1: Given a digraph and a set of source vertices, find shortest path from any vertex in the set to every other vertex.
Multiple-Source Shortest Paths
Given a digraph and a set of source vertices, find shortest path from any vertex in the set to every other vertex.
7
A
Algorithms (Robert Sedgewick, Kevin Wayne/Princeton)
5
0
1
4
6
3
2
7
8
9
Even-Length Paths
Given a digraph G and a source vertex s, design a linear-time algorithm to determine all vertices that are reachable from s via a path with an even number of edges. This path may contain a cycle.
Suppose the source vertex is 7.
Path to 2 is 7-6-2.
Path to 3 is 7-6-2-3-5-2-3.
Path to 5 is 7-6-2-3-5.
8
Q
Algorithms (Robert Sedgewick, Kevin Wayne/Princeton)
5
6
3
2
7
8
9
?: Give a runtime bound for this problem.
?: Consider modifying a graph traversal algorithm.
?: Consider introducing additional edges or vertices.
Q1: Given a digraph G and a source vertex s, design a linear-time algorithm to determine all vertices that are reachable from s via a path with an even number of edges. This path may contain a cycle.
Even-Length Paths
Given a digraph G and a source vertex s, design a linear-time algorithm to determine all vertices that are reachable from s via a path with an even number of edges. This path may contain a cycle.
9
A
Algorithms (Robert Sedgewick, Kevin Wayne/Princeton)
5
6
3
2
7
8
9