Lecture 02 BFS
Lecture 02 BFS
Graphs
Extremely useful tool in modeling problems
Consist of:
Vertices
Vertices can be
Edges D considered “sites”
E
or locations.
C
A Edges represent
F connections.
B
Vertex
Edge
Graph & BFS / Slide 3
Application
• Each vertex represents a city
• Each edge represents a direct
flight between two cities
• A query on direct flights = a
query on whether an edge exists
• A query on how to get to a
location = does a path exist from A
to B
• We can even associate costs to
edges (weighted graphs), then ask
“what is the cheapest path from A
to B”
Graph & BFS / Slide 4
Definition
A graph G=(V, E) consists a set of vertices, V, and a set of edges, E.
Each edge is a pair of (v, w), where v, w belongs to V
If the pair is unordered, the graph is undirected; otherwise it is directed
{a,b} {a,c}
{b,d} {c,d}
{b,e} {c,f}
{e,f}
An undirected graph
Graph & BFS / Slide 5
Terminology
1. If v1 and v2 are connected, they are said to be
adjacent vertices
v1 and v2 are endpoints of the edge {v1, v2}
Terminology
In directed graphs, edges have direction
This means that {v ,v } ≠ {v ,v } . Directed
1 2 2 1
graphs are drawn with arrows between
edges.
This means {A,B} only, not {B,A}
A B
Graph & BFS / Slide 7
Graph Variations
Variations:
A connected graph has a path from every
vertex to every other
A complete graph is a graph in which each pair
of vertices is connected by an edge
Graph & BFS / Slide 8
Graph Variations
Variations:
In an undirected graph:
Edge (u,v) = edge (v,u)
No self-loops
In a directed graph:
Edge (u,v) goes from vertex u to vertex v, notated uv
Graph & BFS / Slide 9
Graph Variations
More variations:
A weighted graph associates weights with
either the edges or the vertices
E.g., a road map: edges might be weighted w/ distance
Graphs
We will typically express running times in
terms of |E| and |V|
If |E| |V|2 the graph is dense
If |E| |V| the graph is sparse
Graph & BFS / Slide 11
Graph Representation
Two popular computer representations of a
graph. Both represent the vertex set and
the edge set, but in different ways.
1. Adjacency Matrix
Use a 2D matrix to represent the graph
2. Adjacency List
Use a 1D array of linked lists
Adjacency Matrix
Graph & BFS / Slide 12
Adjacency List
0 0 1 2 3 4 5 6 7 8 9
0 0 0 0 0 0 0 0 0 1 0
8
1 0 0 1 1 0 0 0 1 0 1
2 9 2 0 1 0 0 1 0 0 0 1 0
1 3 0 1 0 0 1 1 0 0 0 0
4 0 0 1 1 0 0 0 0 0 0
3 7
6 5 0 0 0 1 0 0 1 0 0 0
4 6 0 0 0 0 0 1 0 1 0 0
5
7 0 1 0 0 0 0 1 0 0 0
8 1 0 1 0 0 0 0 0 0 1
9 0 1 0 0 0 0 0 0 1 0
Graph & BFS / Slide 15
0 8
0
1 2 3 7 9
8
2 1 4 8
2 9 3 1 4 5
1 4 2 3
5 3 6
3 7
6 5 7
6
4 7 1 6
5
8 0 2 9
9 1 8
Graph & BFS / Slide 16
deg(v)
vertex v
Graph & BFS / Slide 17
Adjacency Matrix
Always require n2 space
This can waste a lot of space if the number of edges are sparse
Can quickly find if an edge exists
Graph & BFS / Slide 19
Types of paths
Any cycles?
1. {a,c,f,e}
2. {a,b,d,c,f,e}
3. {a, c, d, b, d, c, f, e}
4. {a,c,d,b,a}
5. {a,c,f,e,b,d,c,a}
Graph & BFS / Slide 22
Graph Traversal
Given a graph representation and a vertex s
in the graph, find all paths from s to other
vertices
Two common graph traversal algorithms
Breadth-First Search (BFS)
Find the shortest paths in an unweighted graph
Graph Searching
Given: a graph G = (V, E), directed or
undirected
Goal: methodically explore every vertex and
every edge
Ultimately: build a tree on the graph
Pick a vertex as the root
Choose certain edges to produce a tree
Note: might also build a forest if graph is not
connected
Graph & BFS / Slide 25
Breadth-First Search
“Explore” a graph, turning it into a tree
One vertex at a time
Expand frontier of explored vertices across the
breadth of the frontier
Builds a tree over the graph
Pick a source vertex to be the root
Find (“discover”) its children, then their
children, etc.
Graph & BFS / Slide 26
Breadth-First Search
Again will associate vertex “colors” to guide the
algorithm
White vertices have not been discovered
All vertices start out white
Breadth-First Search
BFS(G, s) {
initialize vertices;
Q = {s}; // Q is a queue (duh); initialize to s
while (Q not empty) {
u = RemoveTop(Q);
for each v u->adj {
if (v->color == WHITE)
v->color = GREY;
v->d = u->d + 1; What does v->d represent?
v->p = u; What does v->p represent?
Enqueue(Q, v);
}
u->color = BLACK;
}
}
Graph & BFS / Slide 28
v w x y
Graph & BFS / Slide 29
0
v w x y
Q: s
Graph & BFS / Slide 30
1 0
1
v w x y
Q: w r
Graph & BFS / Slide 31
1 0 2
1 2
v w x y
Q: r t x
Graph & BFS / Slide 32
1 0 2
2 1 2
v w x y
Q: t x v
Graph & BFS / Slide 33
1 0 2 3
2 1 2
v w x y
Q: x v u
Graph & BFS / Slide 34
1 0 2 3
2 1 2 3
v w x y
Q: v u y
Graph & BFS / Slide 35
1 0 2 3
2 1 2 3
v w x y
Q: u y
Graph & BFS / Slide 36
1 0 2 3
2 1 2 3
v w x y
Q: y
Graph & BFS / Slide 37
1 0 2 3
2 1 2 3
v w x y
Q: Ø
Graph & BFS / Slide 38
Exercise
Source Vertex: G
Graph & BFS / Slide 39