You are on page 1of 86

UNDİRECTED GRAPHS

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

‣ Undirected Graphs
‣ Graph API
‣ Depth-first search
‣ Breadth-first search
Undirected graphs

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

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.

4
Protein-protein interaction network

Reference: Jeong et al, Nature Review | Genetics


5
The Internet as mapped by the Opte Project

http://en.wikipedia.org/wiki/Internet

6
Map of science clickstreams

http://www.plosone.org/article/info:doi/10.1371/journal.pone.0004803 7
10 million Facebook friends

"Visualizing Friendships" by Paul Butler

8
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

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

10
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?


11
UNDİRECTED GRAPHS

‣ Graph API
‣ Depth-first search
‣ Breadth-first search
Graph representation

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

two drawings of the same graph

Caveat. Intuition can be misleading.


13
Graph representation

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

A 0

B C G 1 2 6

symbol table

D E 3 4

F 5

Anomalies.
14
Graph API: sample client

Graph input format.

tinyG.txt
0-6
0-2
0-1
0-5
1-0
2-0
3-5
3-4

12-11
12-9

15
Set-of-edges graph representation

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

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

16
Adjacency-matrix graph representation

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 0 1 1 0 0 1 1 0 0 0 0 0 0
two entries
for each edge 1 1 0 0 0 0 0 0 0 0 0 0 0 0
0
2 1 0 0 0 0 0 0 0 0 0 0 0 0

3 0 0 0 0 1 1 0 0 0 0 0 0 0
1 2 6

4 0 0 0 1 0 1 1 0 0 0 0 0 0

3 4 1 0 0 1 1 0 0 0 0 0 0 0 0
5

5 6 1 0 0 0 1 0 0 0 0 0 0 0 0

7 0 0 0 0 0 0 0 0 1 0 0 0 0
9 10
8 0 0 0 0 0 0 0 1 0 0 0 0 0

7 8
9 0 0 0 0 0 0 0 0 0 0 1 1 1
11 12
10 0 0 0 0 0 0 0 0 0 1 0 0 0

11 0 0 0 0 0 0 0 0 0 1 0 0 1

12 0 0 0 0 0 0 0 0 0 1 0 1 0 17
Adjacency-list graph representation

Maintain vertex-indexed array of lists.

1 2 6

3 4

9 10

7 8
11 12

18
Adjacency-list graph representation: C implementation

//for each edge call addEdge twice


addEdge(v,w);
typedef struct node
addEdge(w,v);
{
struct node *next;
public void addEdge(int v, int w){ int vertex;
node *q;
}node;
//acquire memory for the new node
q=(node*)malloc(sizeof(node));
node * adj [13];
q->vertex=w;
q->next=NULL;
//insert the node to beginning of the
linked list
q->next=adj[v];
adj[v]= q;
}

19
Graph representations

In practice. Use adjacency-lists representation.


• Algorithms based on iterating over vertices adjacent to v.
• Real-world graphs tend to be sparse.
huge number of vertices,
small average vertex degree

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

Two graphs (V = 50)

20
Graph representations

In practice. Use adjacency-lists representation.


• Algorithms based on iterating over vertices adjacent to v.
• Real-world graphs tend to be sparse.
huge number of vertices,
small average vertex degree

edge between iterate over vertices


representation space add edge
v and w? adjacent to v?

list of edges E 1 E E

adjacency matrix V2 1 1 V

adjacency lists E+V 1 degree(v) degree(v)

21
Graph representations

In practice. Use adjacency-lists representation.


• Algorithms based on iterating over vertices adjacent to v.
• Real-world graphs tend to be sparse.
huge number of vertices,
small average vertex degree

edge between iterate over vertices


representation space add edge
v and w? adjacent to v?

list of edges E 1 E E

adjacency matrix V2 1 1 V

adjacency lists E+V 1 degree(v) degree(v)

22
UNDİRECTED GRAPHS

‣ Graph API
‣ Depth-first search
‣ Breadth-first search
Depth-first search

Goal. Systematically search through a graph.

DFS (to visit a vertex v)

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

Typical applications.
• Find all vertices connected to a given source vertex.
• Find a path between two vertices.
Depth-first search

To visit a vertex v :
• Mark vertex v as visited.
• Recursively visit all unmarked vertices adjacent to v.
tinyG.txt
V
13
0 7 8 E
13
0 5
4 3
1 2 6
0 1
9 10
9 12
6 4
5 4
3 4 11 12 0 2
11 12
9 10
5 0 6
7 8
9 11
graph G
5 3

26
Depth-first search

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

0 7 8 v marked[] edgeTo[v]

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

5 8 F –
9 F –
10 F –
11 F –
graph G
12 F –
27
Depth-first search

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

0 7 8 v marked[] edgeTo[v]

0 T –
1 F –
2 F –
1 2 6 9 10
3 F –
4 F –
5 F –
3 4 11 12
6 F –
7 F –

5 8 F –
9 F –
10 F –
11 F –
visit 0
12 F –
28
Depth-first search

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

0 7 8 v marked[] edgeTo[v]

0 T –
1 F –
2 F –
1 2 6 9 10
3 F –
4 F –
5 F –
3 4 11 12
6 T 0
7 F –

5 8 F –
9 F –
10 F –
11 F –
visit 6
12 F –
29
Depth-first search

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

0 7 8 v marked[] edgeTo[v]

0 T –
1 F –
2 F –
1 2 6 9 10
3 F –
4 F –
5 F –
3 4 11 12
6 T 0
7 F –

5 8 F –
9 F –
10 F –
11 F –
visit 6
12 F –
30
Depth-first search

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

0 7 8 v marked[] edgeTo[v]

0 T –
1 F –
2 F –
1 2 6 9 10
3 F –
4 T 6
5 F –
3 4 11 12
6 T 0
7 F –

5 8 F –
9 F –
10 F –
11 F –
visit 4
12 F –
31
Depth-first search

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

0 7 8 v marked[] edgeTo[v]

0 T –
1 F –
2 F –
1 2 6 9 10
3 F –
4 T 6
5 T 4
3 4 11 12
6 T 0
7 F –

5 8 F –
9 F –
10 F –
11 F –
visit 5
12 F –
32
Depth-first search

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

0 7 8 v marked[] edgeTo[v]

0 T –
1 F –
2 F –
1 2 6 9 10
3 T 5
4 T 6
5 T 4
3 4 11 12
6 T 0
7 F –

5 8 F –
9 F –
10 F –
11 F –
visit 3
12 F –
33
Depth-first search

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

0 7 8 v marked[] edgeTo[v]

0 T –
1 F –
2 F –
1 2 6 9 10
3 T 5
4 T 6
5 T 4
3 4 11 12
6 T 0
7 F –

5 8 F –
9 F –
10 F –
11 F –
visit 3
12 F –
34
Depth-first search

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

0 7 8 v marked[] edgeTo[v]

0 T –
1 F –
2 F –
1 2 6 9 10
3 T 5
4 T 6
5 T 4
3 4 11 12
6 T 0
7 F –

5 8 F –
9 F –
10 F –
11 F –
3 done
12 F –
35
Depth-first search

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

0 7 8 v marked[] edgeTo[v]

0 T –
1 F –
2 F –
1 2 6 9 10
3 T 5
4 T 6
5 T 4
3 4 11 12
6 T 0
7 F –

5 8 F –
9 F –
10 F –
11 F –
visit 5
12 F –
36
Depth-first search

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

0 7 8 v marked[] edgeTo[v]

0 T –
1 F –
2 F –
1 2 6 9 10
3 T 5
4 T 6
5 T 4
3 4 11 12
6 T 0
7 F –

5 8 F –
9 F –
10 F –
11 F –
visit 5
12 F –
37
Depth-first search

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

0 7 8 v marked[] edgeTo[v]

0 T –
1 F –
2 F –
1 2 6 9 10
3 T 5
4 T 6
5 T 4
3 4 11 12
6 T 0
7 F –

5 8 F –
9 F –
10 F –
11 F –
5 done
12 F –
38
Depth-first search

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

0 7 8 v marked[] edgeTo[v]

0 T –
1 F –
2 F –
1 2 6 9 10
3 T 5
4 T 6
5 T 4
3 4 11 12
6 T 0
7 F –

5 8 F –
9 F –
10 F –
11 F –
visit 4
12 F –
39
Depth-first search

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

0 7 8 v marked[] edgeTo[v]

0 T –
1 F –
2 F –
1 2 6 9 10
3 T 5
4 T 6
5 T 4
3 4 11 12
6 T 0
7 F –

5 8 F –
9 F –
10 F –
11 F –
visit 4
12 F –
40
Depth-first search

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

0 7 8 v marked[] edgeTo[v]

0 T –
1 F –
2 F –
1 2 6 9 10
3 T 5
4 T 6
5 T 4
3 4 11 12
6 T 0
7 F –

5 8 F –
9 F –
10 F –
11 F –
4 done
12 F –
41
Depth-first search

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

0 7 8 v marked[] edgeTo[v]

0 T –
1 F –
2 F –
1 2 6 9 10
3 T 5
4 T 6
5 T 4
3 4 11 12
6 T 0
7 F –

5 8 F –
9 F –
10 F –
11 F –
6 done
12 F –
42
Depth-first search

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

0 7 8 v marked[] edgeTo[v]

0 T –
1 F –
2 F –
1 2 6 9 10
3 T 5
4 T 6
5 T 4
3 4 11 12
6 T 0
7 F –

5 8 F –
9 F –
10 F –
11 F –
visit 0
12 F –
43
Depth-first search

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

0 7 8 v marked[] edgeTo[v]

0 T –
1 F –
2 T 0
1 2 6 9 10
3 T 5
4 T 6
5 T 4
3 4 11 12
6 T 0
7 F –

5 8 F –
9 F –
10 F –
11 F –
visit 2
12 F –
44
Depth-first search

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

0 7 8 v marked[] edgeTo[v]

0 T –
1 F –
2 T 0
1 2 6 9 10
3 T 5
4 T 6
5 T 4
3 4 11 12
6 T 0
7 F –

5 8 F –
9 F –
10 F –
11 F –
2 done
12 F –
45
Depth-first search

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

0 7 8 v marked[] edgeTo[v]

0 T –
1 F –
2 T 0
1 2 6 9 10
3 T 5
4 T 6
5 T 4
3 4 11 12
6 T 0
7 F –

5 8 F –
9 F –
10 F –
11 F –
visit 0
12 F –
46
Depth-first search

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

0 7 8 v marked[] edgeTo[v]

0 T –
1 T 0
2 T 0
1 2 6 9 10
3 T 5
4 T 6
5 T 4
3 4 11 12
6 T 0
7 F –

5 8 F –
9 F –
10 F –
11 F –
visit 1
12 F –
47
Depth-first search

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

0 7 8 v marked[] edgeTo[v]

0 T –
1 T 0
2 T 0
1 2 6 9 10
3 T 5
4 T 6
5 T 4
3 4 11 12
6 T 0
7 F –

5 8 F –
9 F –
10 F –
11 F –
1 done
12 F –
48
Depth-first search

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

0 7 8 v marked[] edgeTo[v]

0 T –
1 T 0
2 T 0
1 2 6 9 10
3 T 5
4 T 6
5 T 4
3 4 11 12
6 T 0
7 F –

5 8 F –
9 F –
10 F –
11 F –
0 done
12 F –
49
Depth-first search

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

0 7 8 v marked[] edgeTo[v]

0 T –
1 T 0
2 T 0
1 2 6 9 10
3 T 5
4 T 6
5 T 4
3 4 11 12
6 T 0
7 F –

5 8 F –
9 F –
10 F –
11 F –
vertices reachable from 0
12 F –
50
Depth-first search

Goal. Find all vertices connected to s (and a path).

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

void DFS(int i)
{ typedef struct node
{
node *p = adj[i];
struct node *next;
visited[i]=1;
int vertex;
while(p != NULL)
}node;
{
i = p->vertex; //GLOBAL PARAMETERS
if(!visited[i]) node * adj [13];

DFS(i); int visited[13];

p = p->next;
}
}

52
UNDİRECTED GRAPHS

‣ Graph API
‣ Depth-first search
‣ Breadth-first search
Breadth-first search

Repeat until queue is empty:


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

0 2 V
6 E
8
0 5
1 2 4
2 3
1 2 dr
3 0 1
5 4 3 4
3 5
0 2

graph G
adjacency lists
54
Breadth-first search

Repeat until queue is empty:


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

0 2
queue v edgeTo[v]

0 –
1 –
1
2 –
3 –
3 4 –
5 4 5 –

add 0 to queue

55
Breadth-first search

Repeat until queue is empty:


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

0 2
queue v edgeTo[v]

0 –
1 –
1
2 –
3 –
3 4 –
5 4 5 –
0

dequeue 0

56
Breadth-first search

Repeat until queue is empty:


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

0 2
queue v edgeTo[v]

0 –
1 –
1
2 0

3 –
3 4 –
5 4 5 –

dequeue 0

57
Breadth-first search

Repeat until queue is empty:


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

0 2
queue v edgeTo[v]

0 –
1 0

1
2 0
3 –
3 4 –
5 4 5 –
2

dequeue 0

58
Breadth-first search

Repeat until queue is empty:


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

0 2
queue v edgeTo[v]

0 –
1 0
1
2 0
3 –
3 4 –
1
5 4 5 0

2

dequeue 0

59
Breadth-first search

Repeat until queue is empty:


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

0 2
queue v edgeTo[v]

0 –
1 0
1
2 0
5 3 –
3 4 –
1
5 4 5 0
2

0 done

60
Breadth-first search

Repeat until queue is empty:


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

0 2
queue v edgeTo[v]

0 –
1 0
1
2 0
5 3 –
3 4 –
1
5 4 5 0
2

dequeue 2

61
Breadth-first search

Repeat until queue is empty:


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

0 2
queue v edgeTo[v]

0 –
1 0
1
2 0
3 –
3 4 –
5
5 4 5 0
1

dequeue 2

62
Breadth-first search

Repeat until queue is empty:


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

0 2
queue v edgeTo[v]

0 –
1 0
1
2 0
3 –
3 4 –
5
5 4 5 0
1

dequeue 2

63
Breadth-first search

Repeat until queue is empty:


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

0 2
queue v edgeTo[v]

0 –
1 0
1
2 0
3 2

3 4 –
5
5 4 5 0
1

dequeue 2

64
Breadth-first search

Repeat until queue is empty:


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

0 2
queue v edgeTo[v]

0 –
1 0
1
2 0
3 3 2
3 4 2

5
5 4 5 0
1

dequeue 2

65
Breadth-first search

Repeat until queue is empty:


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

0 2
queue v edgeTo[v]

0 –
1 0
1 4
2 0
3 3 2
3 4 2
5
5 4 5 0
1

2 done

66
Breadth-first search

Repeat until queue is empty:


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

0 2
queue v edgeTo[v]

0 –
1 0
1 4
2 0
3 3 2
3 4 2
5
5 4 5 0
1

dequeue 1

67
Breadth-first search

Repeat until queue is empty:


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

0 2
queue v edgeTo[v]

0 –
1 0
1
2 0
4 3 2
3 4 2
3
5 4 5 0
5

dequeue 1

68
Breadth-first search

Repeat until queue is empty:


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

0 2
queue v edgeTo[v]

0 –
1 0
1
2 0
4 3 2
3 4 2
3
5 4 5 0
5

dequeue 1

69
Breadth-first search

Repeat until queue is empty:


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

0 2
queue v edgeTo[v]

0 –
1 0
1
2 0
4 3 2
3 4 2
3
5 4 5 0
5

1 done

70
Breadth-first search

Repeat until queue is empty:


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

0 2
queue v edgeTo[v]

0 –
1 0
1
2 0
4 3 2
3 4 2
3
5 4 5 0
5

dequeue 5

71
Breadth-first search

Repeat until queue is empty:


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

0 2
queue v edgeTo[v]

0 –
1 0
1
2 0
3 2
3 4 2
4
5 4 5 0
3

dequeue 5

72
Breadth-first search

Repeat until queue is empty:


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

0 2
queue v edgeTo[v]

0 –
1 0
1
2 0
3 2
3 4 2
4
5 4 5 0
3

dequeue 5

73
Breadth-first search

Repeat until queue is empty:


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

0 2
queue v edgeTo[v]

0 –
1 0
1
2 0
3 2
3 4 2
4
5 4 5 0
3

5 done

74
Breadth-first search

Repeat until queue is empty:


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

0 2
queue v edgeTo[v]

0 –
1 0
1
2 0
3 2
3 4 2
4
5 4 5 0
3

dequeue 3

75
Breadth-first search

Repeat until queue is empty:


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

0 2
queue v edgeTo[v]

0 –
1 0
1
2 0
3 2
3 4 2
5 4 5 0
4

dequeue 3

76
Breadth-first search

Repeat until queue is empty:


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

0 2
queue v edgeTo[v]

0 –
1 0
1
2 0
3 2
3 4 2
5 4 5 0
4

dequeue 3

77
Breadth-first search

Repeat until queue is empty:


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

0 2
queue v edgeTo[v]

0 –
1 0
1
2 0
3 2
3 4 2
5 4 5 0
4

dequeue 3

78
Breadth-first search

Repeat until queue is empty:


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

0 2
queue v edgeTo[v]

0 –
1 0
1
2 0
3 2
3 4 2
5 4 5 0
4

3 done

79
Breadth-first search

Repeat until queue is empty:


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

0 2
queue v edgeTo[v]

0 –
1 0
1
2 0
3 2
3 4 2
5 4 5 0
4

dequeue 4

80
Breadth-first search

Repeat until queue is empty:


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

0 2
queue v edgeTo[v]

0 –
1 0
1
2 0
3 2
3 4 2
5 4 5 0

dequeue 4

81
Breadth-first search

Repeat until queue is empty:


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

0 2
queue v edgeTo[v]

0 –
1 0
1
2 0
3 2
3 4 2
5 4 5 0

dequeue 4

82
Breadth-first search

Repeat until queue is empty:


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

0 2
queue v edgeTo[v]

0 –
1 0
1
2 0
3 2
3 4 2
5 4 5 0

4 done

83
Breadth-first search

Repeat until queue is empty:


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

0 2
v edgeTo[v]

0 –
1 0
1
2 0
3 2
3 4 2
5 4 5 0

done

84
Breadth-first search

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.

BFS (from source vertex s)

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.

Intuition. BFS examines vertices in increasing distance from s.


85
Breadth-first search properties

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.

1
4
0 2

1 0 2

3
3
5 4
5

standard drawing dist = 0 dist = 1 dist = 2


86

You might also like