Upload
others
View
6
Download
0
Embed Size (px)
Citation preview
Algorithms in Java, 4th Edition · Robert Sedgewick and Kevin Wayne · Copyright © 2008 · October 21, 2008 1:36:19 AM
Undirected Graphs
References:
Algorithms in Java, Chapters 17 and 18 http://www.cs.princeton.edu/algs4/51undirected
! graph API
! maze exploration
! depth-first search
! breadth-first search
! connected components
! challenges
Graph. Set of vertices connected pairwise by edges.
Why study graph algorithms?
• Interesting and broadly useful abstraction.
• Challenging branch of computer science and discrete math.
• Hundreds of graph algorithms known.
• Thousands of practical applications.
2
Undirected graphs
3
Protein interaction network
Reference: Jeong et al, Nature Review | Genetics
4
The Internet as mapped by the Opte Project
http://en.wikipedia.org/wiki/Internet
5
Social networks
Reference: Bearman, Moody and Stovel, 2004image by Mark Newman
high school dating
Reference: Adamic and Adar, 2004
corporate e-mail
7
Graph applications
graph vertex edge
communication telephone, computer fiber optic cable
circuit gate, register, processor wire
mechanical joint rod, beam, spring
financial stock, currency transactions
transportation street intersection, airport highway, airway route
internet class C network connection
game board position legal move
social relationship person, actor friendship, movie cast
neural network neuron synapse
protein network protein protein-protein interaction
chemical compound molecule bond
8
Graph terminology
9
Some graph-processing problems
Path. Is there a path between s and t?
Shortest path. What is the shortest path between s and t?
Cycle. Is there a cycle in the graph?
Euler tour. Is there a cycle that uses each edge exactly once?
Hamilton tour. Is there a cycle that uses each vertex exactly once?
Connectivity. Is there a way to connect all of the vertices?
MST. What is the best way to connect all of the vertices?
Biconnectivity. Is there a vertex whose removal disconnects the graph?
Planarity. Can you draw the graph in the plane with no crossing edges?
Graph isomorphism. Do two adjacency matrices represent the same graph?
Challenge. Which of these problems are easy? difficult? intractable?
10
! graph API
! maze exploration
! depth-first search
! breadth-first search
! connected components
! challenges
Vertex representation.
• This lecture: use integers between 0 and V-1.
• Real world: convert between names and integers with symbol table.
Other issues. Parallel edges, self-loops.
A
G
E
CB
F
D
11
Graph representation
symbol table
0
6
4
21
5
3
12
Graph API
public class Graph graph data type
Graph(int V) create an empty graph with V vertices
Graph(In in) create a graph from input stream
void addEdge(int v, int w) add an edge v-w
Iterable<Integer> adj(int v) return an iterator over the neighbors of v
int V() return number of vertices
String toString() return a string representation
In in = new In();
Graph G = new Graph(in);
StdOut.println(G);
for (int v = 0; v < G.V(); v++)
for (int w : G.adj(v))
/* process edge v-w */
read graph from
standard input
process both
v-w and w-v
% more tiny.txt
7
0 1
0 2
0 5
0 6
3 4
3 5
4 6
Store a list of the edges (linked list or array).
13
Set of edges representation
87
109
1211
0
6
4
21
5
3
0 1
0 2
0 5
0 6
3 4
3 5
4 6
7 8
9 10
9 11
9 12
Maintain a two-dimensional V-by-V boolean array;
for each edge v-w in graph: adj[v][w] = adj[w][v] = true.
0 1 2 3 4 5 6 7 8 9 10 11 12
0
1
2
3
4
5
6
7
8
9
10
11
12
0 1 1 0 0 1 1 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 1 1 0 0 0 0 0 0 0
0 0 0 1 0 1 0 0 0 0 0 0 0
1 0 0 1 1 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 1 0 0 0 0
0 0 0 0 0 0 0 1 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 1 1 1
0 0 0 0 0 0 0 0 0 1 0 0 0
0 0 0 0 0 0 0 0 0 1 0 0 1
0 0 0 0 0 0 0 0 0 1 0 1 0
14
Adjacency-matrix representation
two entries
for each edge
87
109
1211
0
6
4
21
5
3
15
Adjacency-matrix representation: Java implementation
public class Graph
{
private final int V;
private final boolean[][] adj;
public Graph(int V)
{
this.V = V;
adj = new boolean[V][V];
}
public void addEdge(int v, int w)
{
adj[v][w] = true;
adj[w][v] = true;
}
public Iterable<Integer> adj(int v)
{ return new AdjIterator(v); }
}
adjacency matrix
create empty graph
with V vertices
add edge v-w
(no parallel edges)
iterator for v's neighbors
(code for AdjIterator omitted)
16
Adjacency-list representation
Maintain vertex-indexed array of lists (implementation omitted).
5 2 1 6
0
0
5 4
6 5 3
0 4 3
4 0
8
7
10 11 12
9
9 12
9 11
two entries
for each edge
0:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
87
109
1211
0
6
4
21
5
3
Maintain vertex-indexed array of sets.
0:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
{ 1 2 5 6 }
{ 0 }
{ 0 }
{ 4, 5 }
{ 3, 5, 6 }
{ 0, 3, 4 }
{ 0, 4 }
{ 8 }
{ 7 }
{ 10, 11, 12 }
{ 9 }
{ 9, 12 }
{ 1, 9 }
17
Adjacency-set graph representation
two entries
for each edge
87
109
1211
0
6
4
21
5
3
18
Adjacency-set representation: Java implementation
public class Graph
{
private final int V;
private final SET<Integer>[] adj;
public Graph(int V)
{
this.V = V;
adj = (SET<Integer>[]) new SET[V];
for (int v = 0; v < V; v++)
adj[v] = new SET<Integer>();
}
public void addEdge(int v, int w)
{
adj[v].add(w);
adj[w].add(v);
}
public Iterable<Integer> adj(int v)
{ return adj[v]; }
}
adjacency sets
create empty graph
with V vertices
add edge v-w
(no parallel edges)
iterator for v's neighbors
In practice. Use adjacency-set (or adjacency-list) representation.
• Algs all based on iterating over edges incident to v.
• Real-world graphs tend to be “sparse.”
19
Graph representations
representation spaceedge between
v and w?iterate over edges
incident to v?
list of edges E E E
adjacency matrix V2 1 V
adjacency list E + V degree(v) degree(v)
adjacency set E + V log (degree(v)) degree(v)
huge number of vertices,
small average vertex degree
20
! graph API
! maze exploration
! depth-first search
! breadth-first search
! connected components
! challenges
21
Maze exploration
Maze graphs.
• Vertex = intersection.
• Edge = passage.
Goal. Explore every passage in the maze.
22
Trémaux maze exploration
Algorithm.
• Unroll a ball of string behind you.
• Mark each visited intersection by turning on a light.
• Mark each visited passage by opening a door.
First use? Theseus entered labyrinth to kill the monstrous Minotaur;
Ariadne held ball of string.
Claude Shannon (with Theseus mouse)
23
24
Maze exploration
25
Maze exploration
27
! graph API
! maze exploration
! depth-first search
! breadth-first search
! connected components
! challenges
Goal. Systematically search through a graph.
Idea. Mimic maze exploration.
Running time.
• O(E) since each edge examined at most twice.
• Usually less than V to find paths in real graphs.
• Typical applications.
• Find all vertices connected to a given s.
• Find a path from s to t.
Depth-first search
Mark s as visited.
Recursively visit all unmarked vertices v adjacent to s.
DFS (to visit a vertex s)
29
Design goal. Decouple graph data type from graph processing.
Typical client program.
• Create a Graph.
• Pass the Graph to a graph-processing routine, e.g., DFSearcher.
• Query the graph-processing routine for information.
Design pattern for graph processing
// print all vertices connected to s
In in = new In(args[0]);
Graph G = new Graph(in);
int s = 0;
DFSearcher dfs = new DFSearcher(G, s);
for (int v = 0; v < G.V(); v++)
if (dfs.isConnected(v))
StdOut.println(v);
30
Depth-first search (connectivity)
public class DFSearcher
{
private boolean[] marked;
public DFSearcher(Graph G, int s)
{
marked = new boolean[G.V()];
dfs(G, s);
}
private void dfs(Graph G, int v)
{
marked[v] = true;
for (int w : G.adj(v))
if (!marked[w]) dfs(G, w);
}
public boolean isConnected(int v)
{ return marked[v]; }
}
true if connected to s
constructor marks
vertices connected to s
recursive DFS does the work
client can ask whether any
vertex is connected to s
Flood fill
Photoshop “magic wand”
31
Graph-processing challenge 1
Problem. Flood fill.
Assumptions. Picture has millions to billions of pixels.
How difficult?
• Any COS 126 student could do it.
• Need to be a typical diligent COS 226 student.
• Hire an expert.
• Intractable.
• No one knows.
• Impossible.
32
33
Connectivity application: flood fill
Change color of entire blob of neighboring red pixels to blue.
Build a grid graph.
• Vertex: pixel.
• Edge: between two adjacent red pixels.
• Blob: all pixels connected to given pixel.
recolor red blob to blue34
Connectivity application: flood fill
Change color of entire blob of neighboring red pixels to blue.
Build a grid graph.
• Vertex: pixel.
• Edge: between two adjacent red pixels.
• Blob: all pixels connected to given pixel.
recolor red blob to blue
Graph-processing challenge 2
Problem. Is there a path from s to t ?
How difficult?
• Any COS 126 student could do it.
• Need to be a typical diligent COS 226 student.
• Hire an expert.
• Intractable.
• No one knows.
35
Problem. Find a path from s to t ?
Assumption. Any path will do.
How difficult?
• Any COS 126 student could do it.
• Need to be a typical diligent COS 226 student.
• Hire an expert.
• Intractable.
• No one knows.
Graph-processing challenge 3
36
37
Paths in graphs: union find vs. DFS
Is there a path from s to t?
If so, find one.
• Union-find: not much help (run DFS on connected subgraph).
• DFS: easy (stay tuned).
Union-find advantage. Can intermix queries and edge insertions.
DFS advantage. Can recover path itself in time proportional to its length.
method preprocessing time query time space
union-find V + E log* V log* V † V
DFS E + V 1 E + V
† amortized
38
Keeping track of paths with DFS
DFS tree. Upon visiting a vertex v for the first time, remember that you
came from pred[v] (parent-link representation).
Retrace path. To find path between s and v, follow pred[] back from v.
39
Depth-first-search (pathfinding)
public class DFSearcher
{
private int[] pred;
...
public DFSearcher(Graph G, int s)
{
...
pred = new int[G.V()];
for (int v = 0; v < G.V(); v++)
pred[v] = -1;
...
}
private void dfs(Graph G, int v)
{
marked[v] = true;
for (int w : G.adj(v))
if (!marked[w])
{
pred[w] = v;
dfs(G, w);
}
}
public Iterable<Integer> path(int v)
{ /* see next slide */ }
}
add instance variable for parent-link
representation of DFS tree
initialize it in the constructor
set parent link
add method for client
to iterate through path
40
Depth-first-search (pathfinding iterator)
public Iterable<Integer> path(int v)
{
Stack<Integer> path = new Stack<Integer>();
while (v != -1 && marked[v])
{
list.push(v);
v = pred[v];
}
return path;
}
41
DFS summary
Enables direct solution of simple graph problems.
• Find path from s to t.
• Connected components (stay tuned).
• Euler tour (see book).
• Cycle detection (simple exercise).
• Bipartiteness checking (see book).
Basis for solving more difficult graph problems.
• Biconnected components (see book).
• Planarity testing (beyond scope).
!
42
! graph API
! maze exploration
! depth-first search
! breadth-first search
! connected components
! challenge
Depth-first search. Put unvisited vertices on a stack.
Breadth-first search. Put unvisited vertices on a queue.
Shortest path. Find path from s to t that uses fewest number of edges.
Property. BFS examines vertices in increasing distance from s.
43
Breadth-first search
Put s onto a FIFO queue.
Repeat until the queue is empty:
! remove the least recently added vertex v
! add each of v's unvisited neighbors to the queue,
and mark them as visited.
BFS (from source vertex s)
44
Breadth-first search scaffolding
public class BFSearcher
{
private int[] dist;
public BFSearcher(Graph G, int s)
{
dist = new int[G.V()];
for (int v = 0; v < G.V(); v++)
dist[v] = G.V() + 1;
dist[s] = 0;
bfs(G, s);
}
public int distance(int v)
{ return dist[v]; }
private void bfs(Graph G, int s)
{ /* See next slide */ }
}
initialize distances
distances from s
compute distances
answer client query
45
Breadth-first search (compute shortest-path distances)
private void bfs(Graph G, int s)
{
Queue<Integer> q = new Queue<Integer>();
q.enqueue(s);
while (!q.isEmpty())
{
int v = q.dequeue();
for (int w : G.adj(v))
{
if (dist[w] > G.V())
{
q.enqueue(w);
dist[w] = dist[v] + 1;
}
}
}
}
46
BFS application
• Facebook.
• Kevin Bacon numbers.
• Fewest number of hops in a communication network.
ARPANET
47
! graph API
! maze exploration
! depth-first search
! breadth-first search
! connected components
! challenge
Def. Vertices v and w are connected if there is a path between them.
Def. A connected component is a maximal set of connected vertices.
Goal. Preprocess graph to answer queries: is v connected to w?
in constant time
Union-Find? Not quite.
48
Connectivity queries
Vertex Component
0 0
1 1
2 1
3 0
4 0
5 0
6 2
7 0
8 2
9 1
10 0
11 0
12 1
68
19
122
0
7
5
1011
3
3
Goal. Partition vertices into connected components.
49
Connected components
Initialize all vertices v as unmarked.
For each unmarked vertex v, run DFS and identify all vertices
discovered as part of the same connected component.
Connected components
preprocess time query time extra space
E + V 1 V
50
Depth-first search for connected components
public class CCFinder
{
private final static int UNMARKED = -1;
private int components;
private int[] cc;
public CCFinder(Graph G)
{ /* see next slide */ }
public int connected(int v, int w)
{ return cc[v] == cc[w]; }
}
constant-time
connectivity query
component labels
51
Depth-first search for connected components
public CCFinder(Graph G)
{
cc = new int[G.V()];
for (int v = 0; v < G.V(); v++)
cc[v] = UNMARKED;
for (int v = 0; v < G.V(); v++)
if (cc[v] == UNMARKED)
{
dfs(G, v);
components++;
}
}
private void dfs(Graph G, int v)
{
cc[v] = components;
for (int w : G.adj(v))
if (cc[w] == UNMARKED) dfs(G, w);
}
DFS for each component
standard DFS
52
Connected components
63 components
53
Connected components application: image processing
Goal. Read in a 2D color image and find regions of connected pixels
that have the same color.
Input. Scanned image.
Output. Number of red and blue states.
assuming contiguous states
Goal. Read in a 2D color image and find regions of connected pixels
that have the same color.
Efficient algorithm.
• Create grid graph.
• Connect each pixel to neighboring pixel if same color.
• Find connected components in resulting graph.
7 7
7 7
3
3
1 1 1 1 1
1
1
1 1
1 1
11
11 11
11 11 11 11
54
Connected components application: image processing
0 6 6
0 0 0 6 6 6 8
0 0 6 6 4 8
0 0 6 2 11
10 10 10 10 2 11
2 2 2 2 2 11
5 5 5 2 2 11
8 9 9
8 9
11 11 11 11
11 11 11 11
11 11 11 11
11 11 11 11
55
Connected components application: particle detection
Particle detection. Given grayscale image of particles, identify "blobs."
• Vertex: pixel.
• Edge: between two adjacent pixels with grayscale value ! 70.
• Blob: connected component of 20-30 pixels.
Particle tracking. Track moving particles over time.
black = 0
white = 255
56
! graph API
! maze exploration
! depth-first search
! breadth-first search
! connected components
! challenges
Graph-processing challenge 4
Problem. Find a cycle that uses every edge.
Assumption. Need to use each edge exactly once.
How difficult?
• Any COS 126 student could do it.
• Need to be a typical diligent COS 226 student.
• Hire an expert.
• Intractable.
• No one knows.
• Impossible.
57
0-1
0-2
0-5
0-6
1-2
2-3
2-4
3-4
4-5
4-6
6
4
21
3
0
5
0-1-2-3-4-2-0-6-4-5-0
The Seven Bridges of Königsberg. [Leonhard Euler 1736]
Euler tour. Is there a cyclic path that uses each edge exactly once?
Answer. Yes iff connected and all vertices have even degree.
To find path. DFS-based algorithm (see Algs in Java).
58
Bridges of Königsberg
“ … in Königsberg in Prussia, there is an island A, called the
Kneiphof; the river which surrounds it is divided into two branches ...
and these branches are crossed by seven bridges. Concerning these
bridges, it was asked whether anyone could arrange a route in such a
way that he could cross each bridge once and only once. ”
Graph-processing challenge 5
Problem. Find a cycle that visits every vertex.
Assumption. Need to visit each vertex exactly once.
How difficult?
• Any COS 126 student could do it.
• Need to be a typical diligent COS 226 student.
• Hire an expert.
• Intractable.
• No one knows.
• Impossible.
59
0-1
0-2
0-5
0-6
1-2
2-6
3-4
3-5
4-5
4-6
6
4
21
3
0
5
0-5-3-4-6-2-1-0
Graph-processing challenge 6
Problem. Are two graphs identical except for vertex names?
How difficult?
• Any COS 126 student could do it.
• Need to be a typical diligent COS 226 student.
• Hire an expert.
• Intractable.
• No one knows.
• Impossible.
60
0-1
0-6
0-2
4-3
5-3
5-4
0-5
6-4
6
4
21
5
3
0
2-1
2-4
2-0
6-5
5-3
3-6
2-3
6-4
4
6
0
2
3
5
1
Graph-processing challenge 7
Problem. Lay out a graph in the plane without crossing edges?
How difficult?
• Any COS 126 student could do it.
• Need to be a typical diligent COS 226 student.
• Hire an expert.
• Intractable.
• No one knows.
• Impossible.
61
0-2
1-2
2-3
2-4
3-5
3-6
4-6
5-6
4
6
0
2
3
5
1