2. 2
Graphs
v1
v2
v5
v7
v8
v3
v6
v4
● A graph G = (V, E)
■ V: set of vertices (nodes)
■ E: set of edges (links)
● Complete graph
■ There is an edge between every pair of
vertices
● Two kinds of graph
■ Undirected
■ Directed (digraph)
● Undirected graph:
■ E consists of sets of two elements each:
Edge {u, v} is the same as {v, u}
Md. Shafiuzzaman
3. 3
Directed Graphs
v1
v2
v5
v7
v8
v3
v6
v4
● A directed graph, or
digraph:
■ E is set of ordered pairs
■ Even if edge (u, v) is
present, the edge (v, u)
may be absent
● Directed graphs are
drawn with nodes for
vertices and arrows for
edges
Md. Shafiuzzaman
4. 4
Terminology
● Adjacency
■ Vertex w is adjacent to v if and only
(v, w) is in E
● Weight
■ A cost parameter associated with
each edge
● Path
■ A sequence of vertices w1,w2,…,wn,
where there is an edge for each pair
of consecutive vertices
● Length of a path
■ Number of edges along path
■ Length of path of n vertices is n-1
● Cost of a path
■ sum of the weights of the edges
along the path
v1
v2
v5
v7
v8
v3
v6
v4
3
1
-1
5 2
-2
4
Md. Shafiuzzaman
5. Md. Shafiuzzaman
Representing Graphs
● Assume V = {1, 2, …, n}
● An adjacency matrix represents the graph as a
n x n matrix A:
■ A[i, j] = 1 if edge (i, j) E (or weight of edge)
= 0 if edge (i, j) E
8. Md. Shafiuzzaman
Graphs: Adjacency List
● Adjacency list: for each vertex v V, store a
list of vertices adjacent to v
● Example:
■ Adj[1] = {2,3}
■ Adj[2] = {3}
■ Adj[3] = {}
■ Adj[4] = {3}
● Variation: can also keep
a list of edges coming into vertex
1
2 4
3
9. Md. Shafiuzzaman
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
10. Md. Shafiuzzaman
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.
11. Md. Shafiuzzaman
Breadth-First Search
● Again will associate vertex “colors” to guide
the algorithm
■ White vertices have not been discovered
○ All vertices start out white
■ Grey vertices are discovered but not fully explored
○ They may be adjacent to white vertices
■ Black vertices are discovered and fully explored
○ They are adjacent only to black and gray vertices
● Explore vertices by scanning adjacency list of
grey vertices
22. Md. Shafiuzzaman
Breadth-First Search
BFS(G, s) {
initialize vertices;
Q = {s}; // Q is a queue; 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;
v->p = u;
Enqueue(Q, v);
}
u->color = BLACK;
}
}
What does v->p represent?
What does v->d represent?
23. Md. Shafiuzzaman
BFS: The Code Again
BFS(G, s) {
initialize vertices;
Q = {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;
v->p = u;
Enqueue(Q, v);
}
u->color = BLACK;
}
}
What will be the running time?
Touch every vertex: O(V)
u = every vertex, but only once
(Why?)
So v = every vertex
that appears in
some other vert’s
adjacency list
Total running time: O(V+E)
24. Assignment
Given an adjacency matrix representation of a
graph, compute the shortest path from a source
vertex to a goal vertex using Breadth First
Search. In case of a tie, a smaller indexed vertex
should be preferable to a larger indexed vertex.
Md. Shafiuzzaman
25. Input
The first line is the number of test cases.
Thereafter, for every test case, the first line of
input is n, the number of vertices in the graph.
Then n lines of inputs have n integers each,
separated by a space, denoting the adjacency
matrix. The next line of input is the index of
source and goal, the indexing starts from 0.
Md. Shafiuzzaman
27. Output
The first line of output is the cost of shortest path
from source to goal. The second line of output is
the path from source to goal (including both
source and goal).
Md. Shafiuzzaman