U NDIRECTED G RAPHS Acknowledgement: The course slides are adapted - - PowerPoint PPT Presentation

u ndirected g raphs
SMART_READER_LITE
LIVE PREVIEW

U NDIRECTED G RAPHS Acknowledgement: The course slides are adapted - - PowerPoint PPT Presentation

BBM 202 - ALGORITHMS D EPT . OF C OMPUTER E NGINEERING U NDIRECTED G RAPHS Acknowledgement: The course slides are adapted from the slides prepared by R. Sedgewick and K. Wayne of Princeton University. TODAY Undirected Graphs


slide-1
SLIDE 1

BBM 202 - ALGORITHMS

UNDIRECTED GRAPHS


  • DEPT. OF COMPUTER ENGINEERING

Acknowledgement: The course slides are adapted from the slides prepared by R. Sedgewick 
 and K. Wayne of Princeton University.

slide-2
SLIDE 2

TODAY


  • Undirected Graphs
  • Graph API
  • Depth-first search
  • Breadth-first search
  • Connected components
  • Challenges
slide-3
SLIDE 3
  • Graph. Set of vertices connected pairwise by edges.


 Why study graph algorithms?

  • Thousands of practical applications.
  • Hundreds of graph algorithms known.
  • Interesting and broadly useful abstraction.
  • Challenging branch of computer science and discrete math.

3

Undirected graphs

slide-4
SLIDE 4

4

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

slide-5
SLIDE 5

5

Graph terminology

  • Path. Sequence of vertices connected by edges.
  • Cycle. Path whose first and last vertices are the same.

Two vertices are connected if there is a path between them.

cycle of length 5 vertex vertex of degree 3 edge path of length 4 connected components

slide-6
SLIDE 6

6

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 lists represent the same graph? 


  • Challenge. Which of these problems are easy? difficult? intractable?
slide-7
SLIDE 7

UNDIRECTED GRAPHS

  • Graph API
  • Depth-first search
  • Breadth-first search
  • Connected components
  • Challenges
slide-8
SLIDE 8

Graph drawing. Provides intuition about the structure of the graph.
 
 
 
 
 
 
 
 
 
 
 
 


  • Caveat. Intuition can be misleading.

8

Graph representation

two drawings of the same graph

slide-9
SLIDE 9

Vertex representation.

  • This lecture: use integers between 0 and V – 1.
  • Applications: convert between names and integers with symbol table.


 
 
 
 
 
 
 
 
 
 
 Anomalies.

A G E C B F D

9

Graph representation

symbol table 6 4 2 1 5 3

Anomalies parallel edges self-loop

slide-10
SLIDE 10

10

Graph API

public class Graph 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) vertices adjacent to v int V() number of vertices int E() number of edges String toString() string representation In in = new In(args[0]); Graph G = new Graph(in); for (int v = 0; v < G.V(); v++) for (int w : G.adj(v)) StdOut.println(v + "-" + w);

read graph from input stream print out each edge (twice)

slide-11
SLIDE 11

11

Graph input format.

Graph API: sample client

13 13 0 5 4 3 0 1 9 12 6 4 5 4 0 2 11 12 9 10 0 6 7 8 9 11 5 3

tinyG.txt

Input format for V E

% java Test tinyG.txt 0-6 0-2 0-1 0-5 1-0 2-0 3-5 3-4 … 12-11 12-9

In in = new In(args[0]); Graph G = new Graph(in); for (int v = 0; v < G.V(); v++) for (int w : G.adj(v)) StdOut.println(v + "-" + w);

read graph from input stream print out each edge (twice)

slide-12
SLIDE 12

12

Typical graph-processing code

compute the degree of v

public static int degree(Graph G, int v) { int degree = 0; for (int w : G.adj(v)) degree++; return degree; }

compute maximum degree

public static int maxDegree(Graph G) { int max = 0; for (int v = 0; v < G.V(); v++) if (degree(G, v) > max) max = degree(G, v); return max; }

compute average degree

public static double averageDegree(Graph G) { return 2.0 * G.E() / G.V(); }

count self-loops

public static int numberOfSelfLoops(Graph G) { int count = 0; for (int v = 0; v < G.V(); v++) for (int w : G.adj(v)) if (v == w) count++; return count/2; // each edge counted twice }

n

slide-13
SLIDE 13

Maintain a list of the edges (linked list or array).

13

Set-of-edges graph representation

0 1 0 2 0 5 0 6 3 4 3 5 4 5 4 6 7 8 9 10 9 11 9 12 11 12

8 7 10 9 12 11 6 4 2 1 5 3

slide-14
SLIDE 14

Maintain a two-dimensional V-by-V boolean array;
 for each edge v–w in graph: adj[v][w] = adj[w][v] = true.

1 2 3 4 5 6 7 8 9 10 11 12

1 1 1 1

1

1

2

1

3

1 1

4

1 1 1

5

1 1 1

6

1 1

7

1

8

1

9

1 1 1

10

1

11

1 1

12

1 1

14

Adjacency-matrix graph representation

two entries for each edge

10 9 12 11 6 4 2 1 5 3 8 7

slide-15
SLIDE 15

Maintain vertex-indexed array of lists.

15

Adjacency-list graph representation

10 9 12 11 6 4 2 1 5 3

adj[] 1 2 3 4 5 6 7 8 9 10 11 12

5 4 4 9 12 11 9 8 7 9 5 6 3 3 4 11 10 12 6 2 1 5

Bag objects

representations

  • f the same edge

8 7

slide-16
SLIDE 16

16

Adjacency-list graph representation: Java implementation

public class Graph { private final int V; private Bag<Integer>[] adj; public Graph(int V) { this.V = V; adj = (Bag<Integer>[]) new Bag[V]; for (int v = 0; v < V; v++) adj[v] = new Bag<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 lists ( using Bag data type ) create empty graph
 with V vertices add edge v-w
 (parallel edges allowed) iterator for vertices adjacent to v

slide-17
SLIDE 17

In practice. Use adjacency-lists representation.

  • Algorithms based on iterating over vertices adjacent to v.
  • Real-world graphs tend to be sparse.

17

Graph representations

huge number of vertices,
 small average vertex degree

sparse (E = 200) dense (E = 1000)

Two graphs (V = 50)

slide-18
SLIDE 18

In practice. Use adjacency-lists representation.

  • Algorithms based on iterating over vertices adjacent to v.
  • Real-world graphs tend to be sparse.

18

Graph representations

representation space add edge edge between v and w? iterate over vertices adjacent to v? list of edges E 1 E E adjacency matrix V 2 1 * 1 V adjacency lists E + V 1 degree(v) degree(v)

huge number of vertices,
 small average vertex degree * disallows parallel edges

slide-19
SLIDE 19

UNDIRECTED GRAPHS

  • Graph API
  • Depth-first search
  • Breadth-first search
  • Connected components
  • Challenges
slide-20
SLIDE 20

20

Maze exploration

Maze graphs.

  • Vertex = intersection.
  • Edge = passage.
  • Goal. Explore every intersection in the maze.

intersection passage

slide-21
SLIDE 21

Algorithm.

  • Unroll a ball of string behind you.
  • Mark each visited intersection and each visited passage.
  • Retrace steps when no unvisited options.

21

Trémaux maze exploration

slide-22
SLIDE 22
  • Goal. Systematically search through a graph.
  • Idea. Mimic maze exploration.


 
 
 
 
 
 
 Typical applications.

  • Find all vertices connected to a given source vertex.
  • Find a path between two vertices.


Design challenge. How to implement?

Depth-first search

Mark v as visited. Recursively visit all unmarked vertices w adjacent to v. DFS (to visit a vertex v)

slide-23
SLIDE 23

23

Design pattern. Decouple graph data type from graph processing.

  • Create a Graph object.
  • Pass the Graph to a graph-processing routine, e.g., Paths.
  • Query the graph-processing routine for information.

Design pattern for graph processing

Paths paths = new Paths(G, s); for (int v = 0; v < G.V(); v++) if (paths.hasPathTo(v)) StdOut.println(v);

print all vertices
 connected to s

public class Paths Paths(Graph G, int s) find paths in G from source s boolean hasPathTo(int v) is there a path from s to v? Iterable<Integer> pathTo(int v) path from s to v; null if no such path

slide-24
SLIDE 24

To visit a vertex v :

  • Mark vertex v as visited.
  • Recursively visit all unmarked vertices adjacent to v.

8 7 10 9 12 11 6 4 2 1 5 3

Depth-first search

24

graph G 8 7 10 9 12 11 6 4 2 1 5 3 8 7 10 9 12 11 6 4 2 1 5 3

13 13 0 5 4 3 0 1 9 12 6 4 5 4 0 2 11 12 9 10 0 6 7 8 9 11 5 3

tinyG.txt

V E

slide-25
SLIDE 25

To visit a vertex v :

  • Mark vertex v as visited.
  • Recursively visit all unmarked vertices adjacent to v.

4 5 6 2 1 3

Depth-first search

25

vertices reachable from 0 8 7 10 9 12 11 8 7 10 9 12 11 1 2 3 4 5 6 7 8 9 10 11 12

v marked[]

T T T T T T T F F F F F F

edgeTo[v]

– 5 6 4 – – – – – –

slide-26
SLIDE 26
  • Goal. Find all vertices connected to s (and a path).
  • Idea. Mimic maze exploration.


 Algorithm.

  • Use recursion (ball of string).
  • Mark each visited vertex (and keep track of edge taken to visit it).
  • Return (retrace steps) when no unvisited options.


 Data structures.

  • boolean[] marked to mark visited vertices.
  • int[] edgeTo to keep tree of paths.


(edgeTo[w] == v) means that edge v-w taken to visit w for first time

Depth-first search

slide-27
SLIDE 27

27

Depth-first search

public class DepthFirstPaths { private boolean[] marked; private int[] edgeTo; private int s; public DepthFirstSearch(Graph G, int s) { ... 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); edgeTo[w] = v; } } }

marked[v] = true if v connected to s find vertices connected to s recursive DFS does the work edgeTo[v] = previous vertex on path from s to v initialize data structures

slide-28
SLIDE 28

Depth-first search properties

  • Proposition. DFS marks all vertices connected to s in time proportional to


the sum of their degrees.
 Pf.

  • Correctness:
  • if w marked, then w connected to s (why?)
  • if w connected to s, then w marked


(if w unmarked, then consider last edge


  • n a path from s to w that goes from a


marked vertex to an unmarked one)

  • Running time: 


Each vertex connected to s is visited once.

28

set of unmarked vertices no such edge can exist source

v s

set of marked vertices

w x

slide-29
SLIDE 29
  • Proposition. After DFS, can find vertices connected to s in constant time

and can find a path to s (if one exists) in time proportional to its length. 


  • Pf. edgeTo[] is a parent-link representation of a tree rooted at s.

29

Depth-first search properties

public boolean hasPathTo(int v) { return marked[v]; } public Iterable<Integer> pathTo(int v) { if (!hasPathTo(v)) return null; Stack<Integer> path = new Stack<Integer>(); for (int x = v; x != s; x = edgeTo[x]) path.push(x); path.push(s); return path; }

edgeTo[] 1 2 2 0 3 2 4 3 5 3

slide-30
SLIDE 30

UNDIRECTED GRAPHS

  • Graph API
  • Depth-first search
  • Breadth-first search
  • Connected components
  • Challenges
slide-31
SLIDE 31

Repeat until queue is empty:

  • Remove vertex v from queue.
  • Add to queue all unmarked vertices adjacent to v and mark them.

Breadth-first search

31

graph G 4 2 1 5 3

6 8 0 5 2 4 2 3 1 2 0 1 3 4 3 5 0 2

tinyCG.txt

V E

4 2 1 5 3

slide-32
SLIDE 32

Repeat until queue is empty:

  • Remove vertex v from queue.
  • Add to queue all unmarked vertices adjacent to v and mark them.

Breadth-first search

32

add 0 to queue 4 2 1 5 3 4 2 1 5 3 1 2 3 4 5 v edgeTo[v] – – – – – – queue

slide-33
SLIDE 33

Repeat until queue is empty:

  • Remove vertex v from queue.
  • Add to queue all unmarked vertices adjacent to v and mark them.

Breadth-first search

33

4 2 1 5 3 4 2 1 5 3 1 2 3 4 5 v edgeTo[v] – – – – – – queue dequeue 0

slide-34
SLIDE 34

Repeat until queue is empty:

  • Remove vertex v from queue.
  • Add to queue all unmarked vertices adjacent to v and mark them.

Breadth-first search

34

4 2 1 5 3 4 2 1 5 3 1 2 3 4 5 v edgeTo[v] – – – – – – queue dequeue 0 2

slide-35
SLIDE 35

Repeat until queue is empty:

  • Remove vertex v from queue.
  • Add to queue all unmarked vertices adjacent to v and mark them.

Breadth-first search

35

4 2 1 5 3 4 2 1 5 3 1 2 3 4 5 v edgeTo[v] – – – – – queue 2 dequeue 0 2 1

slide-36
SLIDE 36

Repeat until queue is empty:

  • Remove vertex v from queue.
  • Add to queue all unmarked vertices adjacent to v and mark them.

Breadth-first search

36

dequeue 0 4 2 1 5 3 4 2 1 5 3 1 2 3 4 5 v edgeTo[v] – – – – queue 2 1 2 1 5

slide-37
SLIDE 37

Repeat until queue is empty:

  • Remove vertex v from queue.
  • Add to queue all unmarked vertices adjacent to v and mark them.

Breadth-first search

37

0 done 4 2 1 5 3 4 2 1 5 3 1 2 3 4 5 v edgeTo[v] – – – queue 2 1 2 1 5 5

slide-38
SLIDE 38

Repeat until queue is empty:

  • Remove vertex v from queue.
  • Add to queue all unmarked vertices adjacent to v and mark them.

Breadth-first search

38

dequeue 2 4 2 1 5 3 4 2 1 5 3 1 2 3 4 5 v edgeTo[v] – – – queue 2 1 5 1 5

slide-39
SLIDE 39

Repeat until queue is empty:

  • Remove vertex v from queue.
  • Add to queue all unmarked vertices adjacent to v and mark them.

Breadth-first search

39

dequeue 2 4 2 1 5 3 4 2 1 5 3 1 2 3 4 5 v edgeTo[v] – – – queue 1 5 1 5

slide-40
SLIDE 40

Repeat until queue is empty:

  • Remove vertex v from queue.
  • Add to queue all unmarked vertices adjacent to v and mark them.

Breadth-first search

40

dequeue 2 4 2 1 5 3 4 2 1 5 3 1 2 3 4 5 v edgeTo[v] – – – queue 1 5 1 5

slide-41
SLIDE 41

Repeat until queue is empty:

  • Remove vertex v from queue.
  • Add to queue all unmarked vertices adjacent to v and mark them.

Breadth-first search

41

dequeue 2 4 2 1 5 3 4 2 1 5 3 1 2 3 4 5 v edgeTo[v] – – – queue 1 5 1 5 3 2

slide-42
SLIDE 42

Repeat until queue is empty:

  • Remove vertex v from queue.
  • Add to queue all unmarked vertices adjacent to v and mark them.

Breadth-first search

42

dequeue 2 4 2 1 5 3 4 2 1 5 3 1 2 3 4 5 v edgeTo[v] – 2 – queue 1 5 1 5 3 3 4 2

slide-43
SLIDE 43

Repeat until queue is empty:

  • Remove vertex v from queue.
  • Add to queue all unmarked vertices adjacent to v and mark them.

Breadth-first search

43

2 done 4 2 1 5 3 4 2 1 5 3 1 2 3 4 5 v edgeTo[v] – 2 2 queue 1 5 1 5 3 3 4 4

slide-44
SLIDE 44

Repeat until queue is empty:

  • Remove vertex v from queue.
  • Add to queue all unmarked vertices adjacent to v and mark them.

Breadth-first search

44

dequeue 1 4 2 1 5 3 4 1 5 3 1 2 3 4 5 v edgeTo[v] – 2 2 queue 1 5 5 3 3 4 4

slide-45
SLIDE 45

Repeat until queue is empty:

  • Remove vertex v from queue.
  • Add to queue all unmarked vertices adjacent to v and mark them.

Breadth-first search

45

dequeue 1 4 2 1 5 3 4 1 5 3 1 2 3 4 5 v edgeTo[v] – 2 2 queue 5 3 4 5 3 4

slide-46
SLIDE 46

Repeat until queue is empty:

  • Remove vertex v from queue.
  • Add to queue all unmarked vertices adjacent to v and mark them.

Breadth-first search

46

dequeue 1 4 2 1 5 3 4 1 5 3 1 2 3 4 5 v edgeTo[v] – 2 2 queue 5 5 3 3 4 4

slide-47
SLIDE 47

Repeat until queue is empty:

  • Remove vertex v from queue.
  • Add to queue all unmarked vertices adjacent to v and mark them.

Breadth-first search

47

1 done 4 2 1 5 3 4 1 5 3 1 2 3 4 5 v edgeTo[v] – 2 2 queue 5 5 3 3 4 4

slide-48
SLIDE 48

Repeat until queue is empty:

  • Remove vertex v from queue.
  • Add to queue all unmarked vertices adjacent to v and mark them.

Breadth-first search

48

dequeue 5 4 2 1 5 3 4 5 3 1 2 3 4 5 v edgeTo[v] – 2 2 queue 5 3 3 4 4

slide-49
SLIDE 49

Repeat until queue is empty:

  • Remove vertex v from queue.
  • Add to queue all unmarked vertices adjacent to v and mark them.

Breadth-first search

49

dequeue 5 4 2 1 5 3 4 5 3 1 2 3 4 5 v edgeTo[v] – 2 2 queue 3 3 4 4

slide-50
SLIDE 50

Repeat until queue is empty:

  • Remove vertex v from queue.
  • Add to queue all unmarked vertices adjacent to v and mark them.

Breadth-first search

50

dequeue 5 4 2 1 5 3 4 5 3 1 2 3 4 5 v edgeTo[v] – 2 2 queue 3 3 4 4

slide-51
SLIDE 51

Repeat until queue is empty:

  • Remove vertex v from queue.
  • Add to queue all unmarked vertices adjacent to v and mark them.

Breadth-first search

51

5 done 4 2 1 5 3 4 5 3 1 2 3 4 5 v edgeTo[v] – 2 2 queue 3 3 4 4

slide-52
SLIDE 52

Repeat until queue is empty:

  • Remove vertex v from queue.
  • Add to queue all unmarked vertices adjacent to v and mark them.

Breadth-first search

52

dequeue 3 4 2 1 5 3 4 1 2 3 4 5 v edgeTo[v] – 2 2 queue 3 3 4 4

slide-53
SLIDE 53

Repeat until queue is empty:

  • Remove vertex v from queue.
  • Add to queue all unmarked vertices adjacent to v and mark them.

Breadth-first search

53

dequeue 3 4 2 1 5 3 4 1 2 3 4 5 v edgeTo[v] – 2 2 queue 3 4 4

slide-54
SLIDE 54

Repeat until queue is empty:

  • Remove vertex v from queue.
  • Add to queue all unmarked vertices adjacent to v and mark them.

Breadth-first search

54

dequeue 3 4 2 1 5 3 4 1 2 3 4 5 v edgeTo[v] – 2 2 queue 3 4 4

slide-55
SLIDE 55

Repeat until queue is empty:

  • Remove vertex v from queue.
  • Add to queue all unmarked vertices adjacent to v and mark them.

Breadth-first search

55

dequeue 3 4 2 1 5 3 4 1 2 3 4 5 v edgeTo[v] – 2 2 queue 3 4 4

slide-56
SLIDE 56

Repeat until queue is empty:

  • Remove vertex v from queue.
  • Add to queue all unmarked vertices adjacent to v and mark them.

3

Breadth-first search

56

3 done 4 2 1 5 4 1 2 3 4 5 v edgeTo[v] – 2 2 queue 4 4

slide-57
SLIDE 57

Repeat until queue is empty:

  • Remove vertex v from queue.
  • Add to queue all unmarked vertices adjacent to v and mark them.

Breadth-first search

57

dequeue 4 4 2 1 5 3 4 1 2 3 4 5 v edgeTo[v] – 2 2 queue 4

slide-58
SLIDE 58

Repeat until queue is empty:

  • Remove vertex v from queue.
  • Add to queue all unmarked vertices adjacent to v and mark them.

Breadth-first search

58

dequeue 4 4 2 1 5 3 4 1 2 3 4 5 v edgeTo[v] – 2 2 queue

slide-59
SLIDE 59

Repeat until queue is empty:

  • Remove vertex v from queue.
  • Add to queue all unmarked vertices adjacent to v and mark them.

Breadth-first search

59

dequeue 4 4 2 1 5 3 4 1 2 3 4 5 v edgeTo[v] – 2 2 queue

slide-60
SLIDE 60

Repeat until queue is empty:

  • Remove vertex v from queue.
  • Add to queue all unmarked vertices adjacent to v and mark them.

Breadth-first search

60

4 done 4 2 1 5 3 4 1 2 3 4 5 v edgeTo[v] – 2 2 queue

slide-61
SLIDE 61

Repeat until queue is empty:

  • Remove vertex v from queue.
  • Add to queue all unmarked vertices adjacent to v and mark them.

Breadth-first search

61

done 1 2 3 4 5 v edgeTo[v] – 2 2 4 2 1 5 3

slide-62
SLIDE 62

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.

  • Intuition. BFS examines vertices in increasing distance from s.

62

Breadth-first search

Put s onto a FIFO queue, and mark s as visited. 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)

slide-63
SLIDE 63
  • Proposition. BFS computes shortest path (number of edges) from s


in a connected graph in time proportional to E + V. 


  • Pf. [correctness] Queue always consists of zero or more vertices of

distance k from s, followed by zero or more vertices of distance k + 1. 


  • Pf. [running time] Each vertex connected to s is visited once.

Breadth-first search properties

63

4 2 1 5 3

standard drawing

4 2 1 5 3

dist = 0 dist = 1 dist = 2

slide-64
SLIDE 64

64

Breadth-first search

public class BreadthFirstPaths { private boolean[] marked; private boolean[] edgeTo[]; private final int s; … private void bfs(Graph G, int s) { Queue<Integer> q = new Queue<Integer>(); q.enqueue(s); marked[s] = true; while (!q.isEmpty()) { int v = q.dequeue(); for (int w : G.adj(v)) { if (!marked[w]) { q.enqueue(w); marked[w] = true; edgeTo[w] = v; } } } } }

slide-65
SLIDE 65

UNDIRECTED GRAPHS

  • Graph API
  • Depth-first search
  • Breadth-first search
  • Connected components
  • Challenges
slide-66
SLIDE 66
  • Def. Vertices v and w are connected if there is a path between them.

  • Goal. Preprocess graph to answer queries: is v connected to w ? 


in constant time. 
 
 
 
 
 
 
 
 
 
 Depth-first search. [next few slides]

66

Connectivity queries

public class CC CC(Graph G) find connected components in G boolean connected(int v, int w) are v and w connected? int count() number of connected components int id(int v) component identifier for v


slide-67
SLIDE 67

The relation "is connected to" is an equivalence relation:

  • Reflexive: v is connected to v.
  • Symmetric: if v is connected to w, then w is connected to v.
  • Transitive: if v connected to w and w connected to x, then v connected to x.

  • Def. A connected component is a maximal set of connected vertices.


 
 
 
 
 
 
 


  • Remark. Given connected components, can answer queries in constant time.

67

Connected components

v id[v] 0
 1 2 0
 3 4 0
 5 6 0
 7 1 8 1 9 2 10 2 11 2 12 2

8 7 10 9 12 11 6 4 2 1 5 3 3 connected components

slide-68
SLIDE 68
  • Def. A connected component is a maximal set of connected vertices.

68

Connected components

63 connected components

slide-69
SLIDE 69
  • Goal. Partition vertices into connected components.

69

Connected components

Initialize all vertices v as unmarked. For each unmarked vertex v, run DFS to identify all vertices discovered as part of the same component. Connected components

13 13 0 5 4 3 0 1 9 12 6 4 5 4 0 2 11 12 9 10 0 6 7 8 9 11 5 3

tinyG.txt

V E

slide-70
SLIDE 70

To visit a vertex v :

  • Mark vertex v as visited.
  • Recursively visit all unmarked vertices adjacent to v.

8 7 10 9 12 11 6 4 2 1 5 3

Connected components

70

graph G 1 2 3 4 5 6 7 8 9 10 11 12

marked[] v

F F F F F F F F F F F F F 8 7 10 9 12 11 6 4 2 1 5 3 8 7 10 9 12 11 6 4 2 1 5 3

cc[]

– – – – – – – – – – – – –

slide-71
SLIDE 71

To visit a vertex v :

  • Mark vertex v as visited.
  • Recursively visit all unmarked vertices adjacent to v.

8 7 10 9 12 11 6 4 2 1 5 3

Connected components

71

visit 0: check 6, check 2, check 1 and check 5 8 7 10 9 12 11 6 4 2 1 5 3 1 2 3 4 5 6 7 8 9 10 11 12

marked[] v

T F F F F F F F F F F F F

cc[]

– – – – – – – – – – – –

slide-72
SLIDE 72

– – – – – – – – – – – 6 6

To visit a vertex v :

  • Mark vertex v as visited.
  • Recursively visit all unmarked vertices adjacent to v.

4 2 1 5 3

Connected components

72

visit 6: check 0 and check 4 4 2 1 5 3 8 7 10 9 12 11 8 7 10 9 12 11 1 2 3 4 5 6 7 8 9 10 11 12

marked[] v

T F F F F F T F F F F F F

cc[]

slide-73
SLIDE 73

To visit a vertex v :

  • Mark vertex v as visited.
  • Recursively visit all unmarked vertices adjacent to v.

6 4 2 1 5 3

Connected components

73

visit 6: check 0 and check 4 6 4 2 1 5 3 6 8 7 10 9 12 11 8 7 10 9 12 11 – – – – – – – – – – – 1 2 3 4 5 6 7 8 9 10 11 12

marked[] v

T F F F F F T F F F F F F

cc[]

slide-74
SLIDE 74

To visit a vertex v :

  • Mark vertex v as visited.
  • Recursively visit all unmarked vertices adjacent to v.

6 6 4 2 1 5 3

Connected components

74

visit 4: check 5, check 6 and check 3 4 2 1 5 3 4 8 7 10 9 12 11 8 7 10 9 12 11 – – – – – – – – – – 1 2 3 4 5 6 7 8 9 10 11 12

marked[] v

T F F F T F T F F F F F F

cc[]

slide-75
SLIDE 75

4 6 6

To visit a vertex v :

  • Mark vertex v as visited.
  • Recursively visit all unmarked vertices adjacent to v.

2 1 5 3

Connected components

75

visit 5: check 3, check 4 and check 0 2 1 5 3 5 8 7 10 9 12 11 8 7 10 9 12 11 – – – – – – – – – 1 2 3 4 5 6 7 8 9 10 11 12

marked[] v

T F F F T T T F F F F F F

cc[]

slide-76
SLIDE 76

5 4 6 6

To visit a vertex v :

  • Mark vertex v as visited.
  • Recursively visit all unmarked vertices adjacent to v.

2 1 3

Connected components

76

visit 3: check 5 and check 4 2 1 3 8 7 10 9 12 11 8 7 10 9 12 11 – – – – – – – – 1 2 3 4 5 6 7 8 9 10 11 12

marked[] v

T F F T T T T F F F F F F

cc[]

slide-77
SLIDE 77

5 4 6 6

To visit a vertex v :

  • Mark vertex v as visited.
  • Recursively visit all unmarked vertices adjacent to v.

2 1 3

Connected components

77

visit 3: check 5 and check 4 2 1 3 8 7 10 9 12 11 8 7 10 9 12 11 – – – – – – – – 1 2 3 4 5 6 7 8 9 10 11 12

marked[] v

T F F T T T T F F F F F F

cc[]

slide-78
SLIDE 78

To visit a vertex v :

  • Mark vertex v as visited.
  • Recursively visit all unmarked vertices adjacent to v.

5 4 6 6 2 1 3

Connected components

78

3 done 2 1 3 8 7 10 9 12 11 8 7 10 9 12 11 5 – – – – – – – – 1 2 3 4 5 6 7 8 9 10 11 12

marked[] v

T F F T T T T F F F F F F

cc[]

slide-79
SLIDE 79

To visit a vertex v :

  • Mark vertex v as visited.
  • Recursively visit all unmarked vertices adjacent to v.

5 4 6 6 2 1 3

Connected components

79

visit 5: check 3, check 4 and check 0 2 1 8 7 10 9 12 11 8 7 10 9 12 11 5 – – – – – – – – 1 2 3 4 5 6 7 8 9 10 11 12

marked[] v

T F F T T T T F F F F F F

cc[]

slide-80
SLIDE 80

To visit a vertex v :

  • Mark vertex v as visited.
  • Recursively visit all unmarked vertices adjacent to v.

5 4 6 6 2 1 3

Connected components

80

visit 5: check 3, check 4 and check 0 2 1 8 7 10 9 12 11 8 7 10 9 12 11 5 – – – – – – – – 1 2 3 4 5 6 7 8 9 10 11 12

marked[] v

T F F T T T T F F F F F F

cc[]

slide-81
SLIDE 81

To visit a vertex v :

  • Mark vertex v as visited.
  • Recursively visit all unmarked vertices adjacent to v.

5 4 6 6 2 1 3

Connected components

81

5 done 2 1 8 7 10 9 12 11 8 7 10 9 12 11 5 4 – – – – – – – – 1 2 3 4 5 6 7 8 9 10 11 12

marked[] v

T F F T T T T F F F F F F

cc[]

slide-82
SLIDE 82

To visit a vertex v :

  • Mark vertex v as visited.
  • Recursively visit all unmarked vertices adjacent to v.

5 4 6 6 2 1 3

Connected components

82

visit 4: check 5, check 6 and check 3 2 1 8 7 10 9 12 11 8 7 10 9 12 11 4 – – – – – – – – 1 2 3 4 5 6 7 8 9 10 11 12

marked[] v

T F F T T T T F F F F F F

cc[]

slide-83
SLIDE 83

To visit a vertex v :

  • Mark vertex v as visited.
  • Recursively visit all unmarked vertices adjacent to v.

5 4 6 6 2 1 3

Connected components

83

visit 4: check 5, check 6 and check 3 2 1 8 7 10 9 12 11 8 7 10 9 12 11 4 – – – – – – – – 1 2 3 4 5 6 7 8 9 10 11 12

marked[] v

T F F T T T T F F F F F F

cc[]

slide-84
SLIDE 84

To visit a vertex v :

  • Mark vertex v as visited.
  • Recursively visit all unmarked vertices adjacent to v.

4 5 6 6 2 1 3

Connected components

84

4 done 2 1 8 7 10 9 12 11 8 7 10 9 12 11 4 6 – – – – – – – – 1 2 3 4 5 6 7 8 9 10 11 12

marked[] v

T F F T T T T F F F F F F

cc[]

slide-85
SLIDE 85

To visit a vertex v :

  • Mark vertex v as visited.
  • Recursively visit all unmarked vertices adjacent to v.

6 4 5 2 1 3

Connected components

85

6 done 2 1 8 7 10 9 12 11 8 7 10 9 12 11 6 – – – – – – – – 1 2 3 4 5 6 7 8 9 10 11 12

marked[] v

T F F T T T T F F F F F F

cc[]

slide-86
SLIDE 86

To visit a vertex v :

  • Mark vertex v as visited.
  • Recursively visit all unmarked vertices adjacent to v.

4 5 6 2 1 3

Connected components

86

visit 0: check 6, check 2, check 1 and check 5 2 1 8 7 10 9 12 11 8 7 10 9 12 11 – – – – – – – – 1 2 3 4 5 6 7 8 9 10 11 12

marked[] v

T F F T T T T F F F F F F

cc[]

slide-87
SLIDE 87

To visit a vertex v :

  • Mark vertex v as visited.
  • Recursively visit all unmarked vertices adjacent to v.

4 5 6 2 1 3

Connected components

87

visit 2: check 0 2 1 8 7 10 9 12 11 8 7 10 9 12 11 – – – – – – – 1 2 3 4 5 6 7 8 9 10 11 12

marked[] v

T F T T T T T F F F F F F

cc[]

slide-88
SLIDE 88

To visit a vertex v :

  • Mark vertex v as visited.
  • Recursively visit all unmarked vertices adjacent to v.

4 5 6 2 1 3

Connected components

88

2 done 2 1 8 7 10 9 12 11 8 7 10 9 12 11 – – – – – – – 1 2 3 4 5 6 7 8 9 10 11 12

marked[] v

T F T T T T T F F F F F F

cc[]

slide-89
SLIDE 89

To visit a vertex v :

  • Mark vertex v as visited.
  • Recursively visit all unmarked vertices adjacent to v.

4 5 6 2 1 3

Connected components

89

visit 0: check 6, check 2, check 1 and check 5 1 8 7 10 9 12 11 8 7 10 9 12 11 – – – – – – – 1 2 3 4 5 6 7 8 9 10 11 12

marked[] v

T F T T T T T F F F F F F

cc[]

slide-90
SLIDE 90

To visit a vertex v :

  • Mark vertex v as visited.
  • Recursively visit all unmarked vertices adjacent to v.

4 5 6 2 1 3

Connected components

90

visit 1: check 0 1 8 7 10 9 12 11 8 7 10 9 12 11 – – – – – – 1 2 3 4 5 6 7 8 9 10 11 12

marked[] v

T T T T T T T F F F F F F

cc[]

slide-91
SLIDE 91

To visit a vertex v :

  • Mark vertex v as visited.
  • Recursively visit all unmarked vertices adjacent to v.

4 5 6 2 1 3

Connected components

91

1 done 1 8 7 10 9 12 11 8 7 10 9 12 11 – – – – – – 1 2 3 4 5 6 7 8 9 10 11 12

marked[] v

T T T T T T T F F F F F F

cc[]

slide-92
SLIDE 92

To visit a vertex v :

  • Mark vertex v as visited.
  • Recursively visit all unmarked vertices adjacent to v.

4 5 6 2 1 3

Connected components

92

0 done 8 7 10 9 12 11 8 7 10 9 12 11 – – – – – – 1 2 3 4 5 6 7 8 9 10 11 12

marked[] v

T T T T T T T F F F F F F

cc[]

slide-93
SLIDE 93

T T T T T T T F F F F F F

To visit a vertex v :

  • Mark vertex v as visited.
  • Recursively visit all unmarked vertices adjacent to v.

Connected components

93

connected component: 0 1 2 3 4 5 6 8 7 10 9 12 11 8 7 10 9 12 11

connected component

4 5 6 2 1 3 – – – – – – 1 2 3 4 5 6 7 8 9 10 11 12

marked[] v cc[]

slide-94
SLIDE 94

To visit a vertex v :

  • Mark vertex v as visited.
  • Recursively visit all unmarked vertices adjacent to v.

4 5 6 2 1 3

Connected components

94

check 1 2 3 4 5 6 8 7 10 9 12 11 8 7 10 9 12 11 – – – – – – 1 2 3 4 5 6 7 8 9 10 11 12

marked[] v

T T T T T T T F F F F F F

cc[]

slide-95
SLIDE 95

To visit a vertex v :

  • Mark vertex v as visited.
  • Recursively visit all unmarked vertices adjacent to v.

4 5 6 2 1 3

Connected components

95

visit 7: check 8 8 7 10 9 12 11 8 7 10 9 12 11 1 – – – – – 1 2 3 4 5 6 7 8 9 10 11 12

marked[] v

T T T T T T T T F F F F F

cc[]

slide-96
SLIDE 96

To visit a vertex v :

  • Mark vertex v as visited.
  • Recursively visit all unmarked vertices adjacent to v.

4 5 6 2 1 3

Connected components

96

visit 8: check 7 8 7 10 9 12 11 8 7 10 9 12 11 1 1 – – – – 1 2 3 4 5 6 7 8 9 10 11 12

marked[] v

T T T T T T T T T F F F F

cc[]

slide-97
SLIDE 97

To visit a vertex v :

  • Mark vertex v as visited.
  • Recursively visit all unmarked vertices adjacent to v.

4 5 6 2 1 3

Connected components

97

8 done 8 7 10 9 12 11 7 10 9 12 11 8 7 1 1 – – – – 1 2 3 4 5 6 7 8 9 10 11 12

marked[] v

T T T T T T T T T F F F F

cc[]

slide-98
SLIDE 98

To visit a vertex v :

  • Mark vertex v as visited.
  • Recursively visit all unmarked vertices adjacent to v.

7 4 5 6 2 1 3

Connected components

98

7 done 8 10 9 12 11 10 9 12 11 7 1 1 – – – – 1 2 3 4 5 6 7 8 9 10 11 12

marked[] v

T T T T T T T T T F F F F

cc[]

slide-99
SLIDE 99

To visit a vertex v :

  • Mark vertex v as visited.
  • Recursively visit all unmarked vertices adjacent to v.

7 4 5 6 2 1 3

Connected components

99

connected component: 7 8 8 10 9 12 11 10 9 12 11 7 8 1 1 – – – – 1 2 3 4 5 6 7 8 9 10 11 12

marked[] v

T T T T T T T T T F F F F

cc[]

slide-100
SLIDE 100

To visit a vertex v :

  • Mark vertex v as visited.
  • Recursively visit all unmarked vertices adjacent to v.

7 4 5 6 2 1 3

Connected components

100

check 8 8 10 9 12 11 10 9 12 11 1 1 – – – – 1 2 3 4 5 6 7 8 9 10 11 12

marked[] v

T T T T T T T T T F F F F

cc[]

slide-101
SLIDE 101

To visit a vertex v :

  • Mark vertex v as visited.
  • Recursively visit all unmarked vertices adjacent to v.

7 4 5 6 2 1 3

Connected components

101

visit 9: check 11, check 10 and check 12 8 10 9 12 11 10 9 12 11 1 1 2 – – – 1 2 3 4 5 6 7 8 9 10 11 12

marked[] v

T T T T T T T T T T F F F

cc[]

slide-102
SLIDE 102

To visit a vertex v :

  • Mark vertex v as visited.
  • Recursively visit all unmarked vertices adjacent to v.

7 4 5 6 2 1 3

Connected components

102

visit 11: check 9 and check 12 8 10 9 12 11 10 9 12 11 1 1 2 – 2 – 1 2 3 4 5 6 7 8 9 10 11 12

marked[] v

T T T T T T T T T T F T F

cc[]

slide-103
SLIDE 103

To visit a vertex v :

  • Mark vertex v as visited.
  • Recursively visit all unmarked vertices adjacent to v.

7 4 5 6 2 1 3

Connected components

103

visit 11: check 9 and check 12 8 10 9 12 11 10 9 12 11 1 1 2 – 2 – 1 2 3 4 5 6 7 8 9 10 11 12

marked[] v

T T T T T T T T T T F T F

cc[]

slide-104
SLIDE 104

To visit a vertex v :

  • Mark vertex v as visited.
  • Recursively visit all unmarked vertices adjacent to v.

7 4 5 6 2 1 3

Connected components

104

visit 12: check 11 and check 9 8 10 9 12 11 10 9 12 11 1 1 2 – 2 2 1 2 3 4 5 6 7 8 9 10 11 12

marked[] v

T T T T T T T T T T F T T

cc[]

slide-105
SLIDE 105

To visit a vertex v :

  • Mark vertex v as visited.
  • Recursively visit all unmarked vertices adjacent to v.

7 4 5 6 2 1 3

Connected components

105

visit 12: check 11 and check 9 8 10 9 12 11 10 9 12 11 1 1 2 – 2 2 1 2 3 4 5 6 7 8 9 10 11 12

marked[] v

T T T T T T T T T T F T T

cc[]

slide-106
SLIDE 106

To visit a vertex v :

  • Mark vertex v as visited.
  • Recursively visit all unmarked vertices adjacent to v.

7 4 5 6 2 1 3

Connected components

106

12 done 8 10 9 12 11 10 9 11 12 11 1 1 2 – 2 2 1 2 3 4 5 6 7 8 9 10 11 12

marked[] v

T T T T T T T T T T F T T

cc[]

slide-107
SLIDE 107

To visit a vertex v :

  • Mark vertex v as visited.
  • Recursively visit all unmarked vertices adjacent to v.

11 7 4 5 6 2 1 3

Connected components

107

11 done 8 10 9 12 10 9 11 9 1 1 2 – 2 2 1 2 3 4 5 6 7 8 9 10 11 12

marked[] v

T T T T T T T T T T F T T

cc[]

slide-108
SLIDE 108

To visit a vertex v :

  • Mark vertex v as visited.
  • Recursively visit all unmarked vertices adjacent to v.

11 7 4 5 6 2 1 3

Connected components

108

visit 9: check 11, check 10 and check 12 8 10 9 12 10 9 1 1 2 – 2 2 1 2 3 4 5 6 7 8 9 10 11 12

marked[] v

T T T T T T T T T T F T T

cc[]

slide-109
SLIDE 109

To visit a vertex v :

  • Mark vertex v as visited.
  • Recursively visit all unmarked vertices adjacent to v.

11 7 4 5 6 2 1 3

Connected components

109

visit 10: check 9 8 10 9 12 10 9 1 1 2 2 2 2 1 2 3 4 5 6 7 8 9 10 11 12

marked[] v

T T T T T T T T T T T T T

cc[]

slide-110
SLIDE 110

To visit a vertex v :

  • Mark vertex v as visited.
  • Recursively visit all unmarked vertices adjacent to v.

11 7 4 5 6 2 1 3

Connected components

110

10 done 8 10 9 12 9 10 9 1 1 2 2 2 2 1 2 3 4 5 6 7 8 9 10 11 12

marked[] v

T T T T T T T T T T T T T

cc[]

slide-111
SLIDE 111

To visit a vertex v :

  • Mark vertex v as visited.
  • Recursively visit all unmarked vertices adjacent to v.

9 11 7 4 5 6 2 1 3

Connected components

111

9 done 8 10 12 9 1 1 2 2 2 2 1 2 3 4 5 6 7 8 9 10 11 12

marked[] v

T T T T T T T T T T T T T

cc[]

slide-112
SLIDE 112

To visit a vertex v :

  • Mark vertex v as visited.
  • Recursively visit all unmarked vertices adjacent to v.

7 4 5 6 2 1 3

Connected components

112

connected component: 9 10 11 12 8 9 11 10 12 1 1 2 2 2 2 1 2 3 4 5 6 7 8 9 10 11 12

marked[] v

T T T T T T T T T T T T T

cc[]

slide-113
SLIDE 113

To visit a vertex v :

  • Mark vertex v as visited.
  • Recursively visit all unmarked vertices adjacent to v.

7 4 5 6 2 1 3

Connected components

113

check 10 11 12 8 11 12 10 9 1 1 2 2 2 2 1 2 3 4 5 6 7 8 9 10 11 12

marked[] v

T T T T T T T T T T T T T

cc[]

slide-114
SLIDE 114

To visit a vertex v :

  • Mark vertex v as visited.
  • Recursively visit all unmarked vertices adjacent to v.

7 4 5 6 2 1 3

Connected components

114

done 8 11 12 10 9 1 1 2 2 2 2 1 2 3 4 5 6 7 8 9 10 11 12

marked[] v

T T T T T T T T T T T T T

cc[]

slide-115
SLIDE 115

115

Finding connected components with DFS

public class CC { private boolean marked[]; private int[] id; private int count; public CC(Graph G) { marked = new boolean[G.V()]; id = new int[G.V()]; for (int v = 0; v < G.V(); v++) { if (!marked[v]) { dfs(G, v); count++; } } } public int count() public int id(int v) private void dfs(Graph G, int v) }

run DFS from one vertex in each component id[v] = id of component containing v number of components see next slide

slide-116
SLIDE 116

116

Finding connected components with DFS (continued)

public int count() { return count; } public int id(int v) { return id[v]; } private void dfs(Graph G, int v) { marked[v] = true; id[v] = count; for (int w : G.adj(v)) if (!marked[w]) dfs(G, w); }

all vertices discovered in same call of dfs have same id number of components id of component containing v

slide-117
SLIDE 117

UNDIRECTED GRAPHS

  • Graph API
  • Depth-first search
  • Breadth-first search
  • Connected components
  • Challenges
slide-118
SLIDE 118

Graph-processing challenge 1

  • Problem. Is a graph bipartite?


 
 
 
 
 
 How difficult?

  • Any programmer could do it.
  • Typical diligent algorithms student could do it.
  • Hire an expert.
  • Intractable.
  • No one knows.
  • Impossible.

118

0-1 0-2 0-5 0-6 1-3 2-3 2-4 4-5 4-6 6 4 2 1 5 3 0-1 0-2 0-5 0-6 1-3 2-3 2-4 4-5 4-6 6 4 2 1 5 3 { 0, 3, 4 }

slide-119
SLIDE 119

Graph-processing challenge 1

  • Problem. Is a graph bipartite?


 
 
 
 
 
 How difficult?

  • Any programmer could do it.
  • Typical diligent algorithms student could do it.
  • Hire an expert.
  • Intractable.
  • No one knows.
  • Impossible.

119

0-1 0-2 0-5 0-6 1-3 2-3 2-4 4-5 4-6 6 4 2 1 5 3 0-1 0-2 0-5 0-6 1-3 2-3 2-4 4-5 4-6 6 4 2 1 5 3

simple DFS-based solution
 (see textbook)

slide-120
SLIDE 120

Graph-processing challenge 2

  • Problem. Find a cycle.


 
 
 
 How difficult?

  • Any programmer could do it.
  • Typical diligent algorithms student could do it.
  • Hire an expert.
  • Intractable.
  • No one knows.
  • Impossible.

120

0-1 0-2 0-5 0-6 1-3 2-3 2-4 4-5 4-6 6 4 2 1 5 3 6 4 2 1 5 3 0-5-4-6-0

slide-121
SLIDE 121

Graph-processing challenge 2

  • Problem. Find a cycle.

How difficult?

  • Any programmer could do it.
  • Typical diligent algorithms student could do it.
  • Hire an expert.
  • Intractable.
  • No one knows.
  • Impossible.

121

0-1 0-2 0-5 0-6 1-3 2-3 2-4 4-5 4-6 6 4 2 1 5 3

simple DFS-based solution
 (see textbook)

6 4 2 1 5 3

0-5-4-6-0

slide-122
SLIDE 122

The Seven Bridges of Königsberg. [Leonhard Euler 1736] Euler tour. Is there a (general) cycle that uses each edge exactly once? Answer. Yes iff connected and all vertices have even degree. To find path. DFS-based algorithm (see textbook).

122

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. ”

slide-123
SLIDE 123

Graph-processing challenge 3

  • Problem. Find a cycle that uses every edge.
  • Assumption. Need to use each edge exactly once.

How difficult?

  • Any programmer could do it.
  • Typical diligent algorithms student could do it.
  • Hire an expert.
  • Intractable.
  • No one knows.
  • Impossible.

123

0-1 0-2 0-5 0-6 1-2 2-3 2-4 3-4 4-5 4-6 6 4 2 1 5 3 0-1-2-3-4-2-0-6-4-5-0

slide-124
SLIDE 124

Graph-processing challenge 3

  • Problem. Find a cycle that uses every edge.
  • Assumption. Need to use each edge exactly once.

How difficult?

  • Any programmer could do it.
  • Typical diligent algorithms student could do it.
  • Hire an expert.
  • Intractable.
  • No one knows.
  • Impossible.

124

Eulerian tour
 (classic graph-processing problem)

0-1 0-2 0-5 0-6 1-2 2-3 2-4 3-4 4-5 4-6 6 4 2 1 5 3 0-1-2-3-4-2-0-6-4-5-0

slide-125
SLIDE 125

Graph-processing challenge 4

  • Problem. Find a cycle that visits every vertex exactly once.


 
 
 
 How difficult?

  • Any programmer could do it.
  • Typical diligent algorithms student could do it.
  • Hire an expert.
  • Intractable.
  • No one knows.
  • Impossible.

125

0-1 0-2 0-5 0-6 1-2 2-6 3-4 3-5 4-5 4-6 0-5-3-4-6-2-1-0 6 4 2 1 5 3

slide-126
SLIDE 126

Graph-processing challenge 4

  • Problem. Find a cycle that visits every vertex.
  • Assumption. Need to visit each vertex exactly once.

How difficult?

  • Any programmer could do it.
  • Typical diligent algorithms student could do it.
  • Hire an expert.
  • Intractable.
  • No one knows.
  • Impossible.

126

Hamiltonian cycle
 (classical NP-complete problem)

0-1 0-2 0-5 0-6 1-2 2-6 3-4 3-5 4-5 4-6 0-5-3-4-6-2-1-0 6 4 2 1 5 3

slide-127
SLIDE 127

Graph-processing challenge 5

  • Problem. Are two graphs identical except for vertex names?


 
 
 
 How difficult?

  • Any programmer could do it.
  • Typical diligent algorithms student could do it.
  • Hire an expert.
  • Intractable.
  • No one knows.
  • Impossible.

127

0-1 0-2 0-5 0-6 3-4 3-5 4-5 4-6 6 4 2 1 5 3 3 1 5 2 4 6

0↔4, 1↔3, 2↔2, 3↔6, 4↔5, 5↔0, 6↔1

0-4 0-5 0-6 1-4 1-5 2-4 3-4 5-6

slide-128
SLIDE 128

Graph-processing challenge 5

  • Problem. Are two graphs identical except for vertex names?

How difficult?

  • Any programmer could do it.
  • Typical diligent algorithms student could do it.
  • Hire an expert.
  • Intractable.
  • No one knows.
  • Impossible.

128

graph isomorphism is 
 longstanding open problem

0-1 0-2 0-5 0-6 3-4 3-5 4-5 4-6 6 4 2 1 5 3

3 1 5 2 4 6 0-4 0-5 0-6 1-4 1-5 2-4 3-4 5-6

0↔4, 1↔3, 2↔2, 3↔6, 4↔5, 5↔0, 6↔1

slide-129
SLIDE 129

Graph-processing challenge 6

  • Problem. Lay out a graph in the plane without crossing edges?


 
 
 
 How difficult?

  • Any programmer could do it.
  • Typical diligent algorithms student could do it.
  • Hire an expert.
  • Intractable.
  • No one knows.
  • Impossible.

129

1 6 4 2 5 3 0-1 0-2 0-5 0-6 3-4 3-5 4-5 4-6 6 4 2 1 5 3

slide-130
SLIDE 130

Graph-processing challenge 6

  • Problem. Lay out a graph in the plane without crossing edges?

How difficult?

  • Any programmer could do it.
  • Typical diligent algorithms student could do it.
  • Hire an expert.
  • Intractable.
  • No one knows.
  • Impossible.

130

linear-time DFS-based planarity algorithm
 discovered by Tarjan in 1970s
 (too complicated for practitioners)

1 6 4 2 5 3 6 4 2 1 5 3 0-1 0-2 0-5 0-6 3-4 3-5 4-5 4-6