Академический Документы
Профессиональный Документы
Культура Документы
BFS
DFS
Euler tour
Princeton University COS 226 Algorithms and Data Structures Spring 2004 Kevin Wayne http://www.Princeton.EDU/~cos226 2
n Tree, forest. H I
games board positions legal moves L M
n Connected, connected component.
social relationship people, actors friendships, movie casts
neural networks neurons synapses
3 6
A Few Graph Problems Graph ADT in Java
7 8
adjacency matrix
9 10
Adjacency Matrix: Java Implementation Iterator
Iterator.
public class Graph { n Client needs way to iterate through elements of adjacency list.
private int V; // number of vertices n Graph implementation doesn't want to reveal details of list.
private int E; // number of edges
private boolean[][] adj; // adjacency matrix n Design pattern: give client just enough to iterate.
11 12
Iterator.
public IntIterator neighbors(int v) { n Java uses interface Iterator with all of its collection data types.
return new AdjMatrixIterator(v);
} n Its method next returns an Object instead of an int.
n Need to import java.util.Iterator and java.util.ArrayList.
private class AdjMatrixIterator implements IntIterator {
int v, w = 0;
AdjMatrixIterator(int v) { this.v = v; } ArrayList list = new ArrayList();
...
public boolean hasNext() { does v have another neighbor? list.add(value);
if (w == V) return false; ...
if (adj[v][w]) return true; Iterator i = list.iterator();
for (w = w; w < V; w++) while(i.hasNext()) {
if (adj[v][w]) return true; System.out.println(i.next());
return false; }
}
13 14
Adjacency List Representation Adjacency List: Java Implementation
private class AdjListIterator implements IntIterator { Edge between Edge from v Enumerate
Representation Space
AdjList x; v and w? to anywhere? all edges
AdjListIterator(AdjList x) { this.x = x; }
Adjacency matrix Q(V 2 ) Q(1) O(V) Q(V 2 )
public boolean hasNext() {
does v have another neighbor? Adjacency list Q(E + V) O(E) Q(1) Q(E + V)
return x != null;
}
17 18
Graph Search Connected Components
19 20
Challenge: implement the game of Minesweeper. Challenge: read in a 2D color image and find regions of connected
pixels that have the same color.
Critical subroutine.
n User selects a cell and program reveals how many adjacent mines.
n If zero, reveal all adjacent cells.
n If any newly revealed cells have zero adjacent mines, repeat.
Original Labeled
21 22
Connected Components Application: Image Processing Depth First Search: Connected Components
23 24
25 26
Graphs and Mazes Breadth First Search
Maze graphs. Graph search. Visit all nodes and edges of graph.
n Vertices = intersections Depth-first search. Put unvisited nodes on a STACK.
n Edges = corridors. Breadth-first search. Put unvisited nodes on a QUEUE.
Solution = BFS.
n Initialize dist[v] = , dist[s] = 0.
n When considering edge v-w:
if w is marked, then ignore
otherwise else set dist[w] = dist[v] + 1,
pred[w] = v, and add w to the queue
27 28
29 30
Related Graph Search Problems Bridges of Knigsberg
Path. Is there a path from s to t? Leonhard Euler, The Seven Bridges of Knigsberg, 1736.
n Solution: DFS, BFS, or PFS.
31 32
How to find an Euler tour (assuming graph is Eulerian). How to find an Euler tour (assuming graph is Eulerian).
n Start at some vertex v and repeatedly follow unused edge until you n Start at some vertex v and repeatedly follow unused edge until you
return to v. return to v.
always possible since all vertices have even degree always possible since all vertices have even degree
n Find additional cyclic paths using remaining edges and splice back n Find additional cyclic paths using remaining edges and splice back
into original cyclic path. into original cyclic path.
3 4 3 4 3 4
5 5 5
33 34
Euler Tour: Implementation