Download Graph Algorithms: Applications and Importance and more Slides Data Representation and Algorithm Design in PDF only on Docsity!
Undirected Graphs
2
Undirected Graphs
Graph. Set of objects with pairwise connections.
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.
3
Graph Applications
communication Graph telephones, computers Vertices Edges fiber optic cables circuits gates, registers, processors wires mechanical joints rods, beams, springs hydraulic reservoirs, pumping stations pipelines financial stocks, currency transactions transportation street intersections, airports highways, airway routes scheduling tasks precedence constraints software systems functions function calls internet web pages hyperlinks games board positions legal moves social relationship people, actors friendships, movie casts neural networks neurons synapses protein networks proteins protein-protein interactions chemical compounds molecules bonds 4
September 11 Hijackers and Associates
R hettfpe:r/e/nwcwew: (^) .Vafirldstism Konrdebasy.org/issues/issue 7 _ 4 /krebs
5
Power Transmission Grid of Western US
Reference: Duncan Watts 6
Protein Interaction Network
Reference: Jeong et al, Nature Review | Genetics 8
Graph Terminology
9
Some Graph Problems
Path. Is there a path between s to t?
Shortest path. What is the shortest path between s and t?
Longest path. What is the longest simple 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?
Isomorphism. Do two adjacency matrices represent the same graph?
14
Adjacency Matrix Representation: Java Implementation
public class Graph { private int V; // number of vertices private boolean[][] adj; // adjacency matrix // empty graph with V vertices public Graph(int V) { this.V = V; this.adj = new boolean[V][V]; } // insert edge v-w, no parallel edges public void insert(int v, int w) { adj[v][w] = true; adj[w][v] = true; } // return iterator for neighbors of v public Iterable adj(int v) { return new AdjIterator(v); } } 15
Adjacency Matrix Iterator
private class AdjIterator implements Iterator, Iterable { int v, w = 0 ; AdjIterator(int v) { this.v = v; } public boolean hasNext() { while (w < V) { if (adj[v][w]) return true; w++; } return false; } public int next() { if (!hasNext()) throw new NoSuchElementException(); return w++; } public Iterator iterator() { return this; } } does v have another neighbor w? 16
Adjacency List Representation
Adjacency list.
! Vertex indexed array of lists.
! Two representations of each undirected edge.
0: 5 2 1 6 1: 0 2: 0 3: 5 4 4: 6 5 3 5: 0 4 3 6: 4 0 7: 8 8: 7 9: 10 11 12 10: 9 11: 9 12 12: 9 11 0 6 4 1 2 5 3 (^7 ) 9 10 (^811) 17
Adjacency List Representation: Java Implementation
public class Graph { private int V; // # vertices private Sequence[] adj; // adjacency lists public Graph(int V) { this.V = V; adj = (Sequence[]) new Sequence[V]; for (int v = 0 ; v < V; v++) adj[v] = new Sequence(); } // insert v-w, parallel edges allowed public void insert(int v, int w) { adj[v].add(w); adj[w].add(v); } public Iterable adj(int v) { return adj[v]; } }
18
Graph Representations
Graphs are abstract mathematical objects.
! ADT implementation requires specific representation.
! Efficiency depends on matching algorithms to representations.
Graphs in practice. [use adjacency list representation]
! Real world graphs are sparse.
! Bottleneck is iterating over edges incident to v.
Representation Space Adjacency matrix V 2 Adjacency list E + V Edge between v and w? 1 degree(v) Iterate over edges incident to v? V degree(v) List of edges E E E 19
Maze Exploration
Claude Shannon (with Theseus mouse) 20
Maze Exploration
Maze graphs.
! Vertex = intersections.
! Edge = passage.
Goal. Explore every passage in the maze.
21
Trémaux Maze Exploration
Trémaux maze exploration.
! Unroll a ball of string behind you.
! Mark each visited intersection by turning on a light.
! Mark each visited passage by opening a door.
History. Theseus entered labyrinth to kill the monstrous Minotaur;
Ariadne held ball of string.
26
Depth First Search
public class DFSearcher { private boolean[] marked; public DFSearcher(Graph G, int s) { marked = new boolean[G.V()]; dfs(G, s); } // depth first search from v private void dfs(Graph G, int v) { marked[v] = true; for (int w : G.adj(v)) if (!marked[w]) dfs(G, w); } public boolean isReachable(int v) { return marked[v]; } } 27
Reachability Application: Flood Fill
Flood fill. Given lime green pixel in an image, change color of entire
blob of neighboring lime pixels to blue.
! Vertex: pixel.
! Edge: between two adjacent lime pixels.
! Blob: all pixels reachable from chosen lime pixel.
recolor lime green blob to blue 29
Paths
Path. Is there a path from s to t? If so, find one.
31
Paths
Path. Is there a path from s to t? If so, find one.
UF advantage. Can intermix query and edge insertion.
DFS advantage. Can recover path itself in same running time.
Method Preprocess Time Union Find E log* V † DFS E + V Query Time log* V † 1 Space V V + E † amortized
32
Keep Track of Path
DFS tree. Upon visiting a vertex v for
the first time, remember from where
you came pred[v].
Retrace path. To find path between
s and v, follow pred[] values back from v.
33
Find Path
public class DFSearcher { // initialize pred[v] to - 1 for all v 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); } } // return path from s to v public Iterable path(int v) { Stack list = new Stack(); while (v != - 1 && marked[v]) { list.push(v); v = pred[v]; } return list; } } 34
DFS Summary
Enables direct solution of simple graph problems.
! Find path between s to t.
! Connected components.
! Euler tour.
! Cycle detection.
! Bipartiteness checking.
Basis for solving more difficulty graph problems.
! Biconnected components.
! Planarity testing.
35
Shortest Path
40
Connected Components
41
Connectivity Queries
Def. Vertices v and w are connected if there is a path between them.
Property. Symmetric and transitive.
Goal. Preprocess graph to answer queries: is v connected to w?
Brute force. Run DFS from each vertex v: quadratic time and space.
H A K L E F D G M J C B I Vertex Component A 0 B 1 C 1 D 0 E 0 F 0 G 2 H 0 I 2 J 1 K 0 L 0 M 1 42
Connectivity Queries
Def. Vertices v and w are connected if there is a path between them.
Property. Symmetric and transitive.
Goal. Preprocess graph to answer queries: is v connected to w?
Connected component. Maximal set of mutually connected vertices.
H A K L E F D G M J C B I Vertex Component A 0 B 1 C 1 D 0 E 0 F 0 G 2 H 0 I 2 J 1 K 0 L 0 M 1 43
Goal. Partition vertices into connected components.
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 E + V Query Time 1 Space V
44
Depth First Search: Connected Components
public class CCFinder { private int components; private int[] cc; public CCFinder(Graph G) { cc = new int[G.V()]; for (int v = 0 ; v < G.V(); v++) cc[v] = - 1 ; for (int v = 0 ; v < G.V(); v++) if (cc[v] == - 1 ) { dfs(G, v); components++; } } // depth first search from v private void dfs(Graph G, int v) { cc[v] = components; for (int w : G.adj(v)) if (cc[w] == - 1 ) dfs(G, w); } public int connected(int v, int w) { return cc[v] == cc[w]; } } are v and w in same connected component? unmarked 45
Connected Components
63 components 46
Connected Components Application: Image Processing
Goal. Read in a 2D color image and find regions of connected pixels
that have the same color.
original (^) labeled 47
Connected Components Application: Image Processing
Goal. Read in a 2D color image and find regions of connected pixels
that have the same color.
Efficient algorithm.
! Connect each pixel to neighboring pixel if same color.
! Find connected components in resulting graph.
0 1 1 1 1 1 6 6 0 0 0 1 6 6 6 8 3 0 0 1 6 6 4 8 3 0 0 1 1 6 2 11 10 10 10 10 1 1 2 11 7 7 2 2 2 2 2 11 7 7 5 5 5 2 2 11 8 9 9 11 8 11 9 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11