Graphs
Graphs
1 2
3 4
1 2
3 4
Graph Terminology
• The degree of a vertex in an undirected graph is
the number of edges that leave/enter the vertex.
• The degree of a vertex in a directed graph is the
same, but we distinguish between in-degree and
out-degree. Degree = in-degree + out-degree.
• A path from u to z is <u,v,w,x,y,z> and denoted
uvwxyz
• The running time of a graph algorithm expressed
in terms of the cardinality of E and V, where E = |
E| and V=|V|; e.g. G=O(EV) is |E| * |V|
Implementing a Graph
• Implement a graph in three ways:
– Adjacency List
– Adjacency-Matrix
– Pointers/memory for each node (actually a form
of adjacency list)
Adjacency List
• List of pointers for each vertex
1 2
1 2 3
2
5 3 4
4 1
3 4 5 2 4
Undirected Adjacency List
1 2
1 2 3 4
2 1 5
5
3 4 1
4 1 3
3 4 5 2 4
Adjacency List
• The sum of the lengths of the adjacency
lists is 2|E| in an undirected graph, and |E| in
a directed graph.
• The amount of memory to store the array
for the adjacency list is
O(max(V,E))=O(V+E).
Adjacency Matrix
1 2 3 4 5
1 2
1 0 1 1 0 0
2 0 0 0 0 0
5
3 0 0 0 1 0
3 4 1 0 0 0 0
4
5 0 1 0 1 0
Undirected Adjacency Matrix
1 2 3 4 5
1 2
1 0 1 1 1 0
5
2 1 0 0 0 1
3 1 0 0 1 0
3 4 4 1 0 1 0 1
5 0 1 0 1 0
Adjacency Matrix vs. List?
• The matrix always uses Θ(v2) memory. Usually
easier to implement and perform lookup than an
adjacency list.
• Sparse graph: very few edges.
• Dense graph: lots of edges. Up to O(v2) edges if
fully connected.
• The adjacency matrix is a good way to represent a
weighted graph. In a weighted graph, the edges
have weights associated with them. Update matrix
entry to contain the weight. Weights could
indicate distance, cost, etc.
Introductory Remarks
• Given a path v1..vn, if v1 = vn, and the edges
don’t repeat, it is a circuit; if the vertices in
a circuit are different, it is a cycle
• A complete graph of n vertices, denoted Kn,
has exactly one edge between each pair of
vertices
• The edge count = = = = O
Searching a Graph
• Search: The goal is to methodically explore
every vertex and every edge; perhaps to do
some processing on each.
• For the most part in our algorithms we will
assume an adjacency-list representation of
the input graph.
Breadth First Search
• Example 1: Tree. This is a special case of a graph.
1
2 3 4
5 6 7 8
Breadth First Search
• Example 2: Directed Graph
1
2 5 6
3 4 7 8
Depth First Search
• Example 2: DFS on directed graph.
14
2
10
15
3 8
1. Single-source shortest path. Find the shortest distance from a source vertex s to every other vertex in
the graph.
2. Single-destination shortest path. Find a shortest path to a given destination vertex t from every vertex
v. This is just the reverse of single-source.
3. Single-pair shortest path. Find a shortest path between a pair of vertices. No algorithm is known for
this problem that runs asymptotically faster than the best single-source algorithms.
4. All-pairs shortest path. Find the shortest path between every vertex in the graph.
In other words, the shortest path from S to X is the minimum over all paths that go
from S to U, then have an edge from U to X, where U is some vertex in S.
How it Works: It works by maintaining a distance estimate D(X) for all vertices X in
V. The elements of D are initialized to the value INFINITE. Vertices are processed in
order of distance from S. Whenever a vertex V is processed, D(X) is updated for every
neighbor X of V.
https://opendsa-server.cs.vt.edu/ODSA/Books/Everything/html/
GraphShortest.html
https://www.programiz.com/dsa/dijkstra-algorithm
https://visualgo.net/en/sssp
Summary - Take-home Messages
• Given a weighted directed acyclic graph, we can find
the shortest distance between two vertices by:
– starting with a trivial path containing the initial
vertex
– growing this path by always going to the next
vertex which has the shortest current path
Dijkstra’s Algorithm time complexity in the worst-
https://visualgo.net/en/sssp
BFS(s) ; s is our source vertex
for each u ÎV - {s} ; Initialize unvisited vertices to ¥
do d[u] ¬ ¥
d[s] ¬ 0 ; distance to source vertex is 0
Q.add(s) ; Queue of vertices to visit
while Q not empty do
u = Q.remove()
for each v Î Adj[u] do ; Get adjacent vertices
if d[v]= ¥
then d[v] ¬ d[u]+1 ; Increment depth
Q.add(v) ; Add to nodes to explore
a d
b e
f i
g h
BFS Example
• Final State shown Can create tree out of
0 3
order we visit nodes
a d
1 a 0
c
1 2
b e
b c 1
2 3
f i
g h e f 2
3 3
d h i g 3
DFS Algorithm
https://opendsa-server.cs.vt.edu/ODSA/Books/Everything/html/Graph
Traversal.html
• Pseudocode
DFS(s)
for each vertex u V
do color[u] White ; not visited
time 1 ; time stamp
for each vertex u V
do if color[u]=White
then DFS-Visit(u,time)
DFS-Visit(u,time)
color[u] Gray ; in progress nodes
d[u] time ; d=discover time
time time+1
for each v Adj[u] do
if color[u]=White
then DFS-Visit(v,time)
color[u] Black
f[u] time time+1 ; f=finish time
DFS(s)
for each vertex u V
do color[u] White ; not visited
time 1 ; time stamp
for each vertex u V
do if color[u]=White
then DFS-Visit(u,time)
DFS-Visit(u,time)
color[u] Gray ; in progress nodes
d[u] time ; d=discover time
time time+1
for each v Adj[u] do
if color[u]=White
then DFS-Visit(v,time)
color[u] Black
f[u] time time+1 ; f=finish time
a d
b e
f i
g h
DFS Example
• Result (start/finish time): Tree:
a
10/11
1/18
d
a c
2/17
c
5/6 f
9/14
b e 12/13
3/16
g h
i
f
g h b e
4/7 8/15
d i
DFS Example
• What if some nodes are unreachable? We still visit those
nodes in DFS. Consider if c-e, f-h links were reversed.
Then we end up with two separate trees
– Still visit all vertices and get a forest: a set of unconnected graphs
without cycles (a tree is a connected graph without cycles).
11/12
1/10
d
a
2/9
c
5/6
13/18
b
e 15/16
3/8
i
f
g h
4/7 14/17
What is next
46