Graphs
Graph
Directed vs Undirected Graph
Acyclic vs Cyclic Graph
Backedge
Breadth First Traversal
Depth First Traversal
Detect Cycle in a Directed Graph
Matrix and List Representation of Graph
Graph data structure consists of a A Node or Vertices
finite set of vertices or nodes. The
Edge
interconnected objects are B C
represented by points termed as
vertices, and the links that connect
the vertices are called edges.
D E
F
Vertex: Each node of the graph is Node or Vertices
A
represented as a vertex.
Edge
V={A,B,C,D,E,F} B C
Edge: Edge represents a path
between two vertices or a line
D E
between two vertices.
E={AB,AC,BD,BE,CE,DE,DF,EF}
F
A A
B C B C
Directed Graph Undirected Graph
A A
B C B C
Acyclic Graph Cyclic Graph
Backedge is an edge that is from node to itself
(selfloop), or one to its ancestor.
A
A
B
Graph terminology
• Adjacent nodes: two nodes are adjacent if
they are connected by an edge
5 is adjacent to 7
7 is adjacent from 5
• Path: a sequence of vertices that connect
two nodes in a graph
• Complete graph: a graph in which every
vertex is directly connected to every other
vertex
Graph terminology (cont.)
• What is the number of edges in a complete
directed graph with N vertices?
N * (N-1)=N^2-N
Graph terminology (cont.)
• What is the number of edges in a complete
undirected graph with N vertices?
N * (N-1) / 2
Graph terminology (cont.)
• Weighted graph: a graph in which each edge
carries a value
Graph Definitions: Path
• A path is a sequence of vertices w1, w2, w3, ....wn such that (wi,
wi+1) e E
• Length of a path = # edges in the path
• A loop is an edge from a vertex onto itself. It is denoted by (v,
v)
• A simple path is a path where no vertices are repeated along
the path
• A cycle is a path with at least one edge such that the first and
last vertices are the same, i.e. w1 = wn
Graph Definitions: Connectedness
• A graph is said to be connected if there is a path from
every vertex to every other vertex.
• A connected graph is strongly connected if it is a
connected graph as well as a directed graph
• A connected graph is weakly connected if it is
– a directed graph that is not strongly connected, but,
– the underlying undirected graph is connected
2
2
1 3
1 3
Strong or Weak?
6 4
6 4
5 5
Applications of Graphs
• Driving Map
– Edge = Road
– Vertex = Intersection
– Edge weight = Time required to cover the road
• Airline Traffic
– Vertex = Cities serviced by the airline
– Edge = Flight exists between two cities
– Edge weight = Flight time or flight cost or both
• Computer networks
– Vertex = Server nodes
– Edge = Data link
– Edge weight = Connection speed
• CAD/VLSI
Graph implementation
• Array-based implementation
– A 1D array is used to represent the vertices
– A 2D array (adjacency matrix) is used to
represent the edges
Representing Graphs: Adjacency Matrix
• Adjacency Matrix
– Two dimensional matrix of size n x n where n is the
number of vertices in the graph
– a[i, j] = 0 if there is no edge between vertices i and j
– a[i, j] = 1 if there is an edge between vertices i and j
– Undirected graphs have both a[i, j] and a[j, i] = 1 if there
is an edge between vertices i and j
– a[i, j] = weight for weighted graphs
• Space requirement is Q(N2)
• Problem: The array is very sparsely populated. For
example if a directed graph has 4 vertices and 3 edges,
the adjacency matrix has 16 cells only 3 of which are 1
Array-based implementation
Representing Graphs: Adjacency List
• Adjacency List
– Array of lists
– Each vertex has an array entry
– A vertex w is inserted in the list for vertex v if there
is an outgoing edge from v to w
– Space requirement = Q(E+V)
– Sometimes, a hash-table of lists is used to implement
the adjacency list when the vertices are identified by
a name (string) instead of an integer
List Example
2 1 2
1 3 2 5
3
4
6 4 6
5
5 6 3 4
Graph Adjacency List
• An adjacency list for a weighted graph should contain two elements in
the list nodes – one element for the vertex and the second element for
the weight of that edge
• Good for sparse graphs
• Vertices adjacent to another vertex can be found quickly
Cost Cycle
• A negative cost cycle is a cycle such that the sum of
the costs of the edges is negative
• The more we cycle through a negative cost cycle, the
lower the cost of the cycle becomes
1 3 Cost of the path v1-v5
1 4 2
2 • No traversal of cycle: 6
4
-12 • One traversal of cycle: 0
5 • Two traversals of cycle: -6
5 • Three traversals of cycle: -12
...
• Negative cost cycles are not allowed when we traverse a graph to
find the weighted shortest path
Search: Look for a given node
Traversal: Always visit all nodes
Breadth First Traversal
Breadth First Traversal (BFT) algorithm traverses all nodes
on graph in a breadthward motion and uses a queue to
remember to get the next vertex.
Layers Layer1
A
Layer2
B C
D E Layer3
F Layer4
• BFT C++ Code: https://www.geeksforgeeks.org/breadth-first-search-or-bfs-for-a-graph/
while(!queue.empty())
{
// Dequeue a vertex from queue and print it
s = queue.front();
cout << s << " ";
queue.pop_front();
// Get all adjacent vertices of the dequeued
// vertex s. If a adjacent has not been visited,
// then mark it visited and enqueue it
for (i = adj[s].begin(); i != adj[s].end(); ++i)
{
if (!visited[*i])
{
visited[*i] = true;
queue.push_back(*i);
}
}
}
Visited=
A
A B C D E F
0 0 0 0 0 0 B C
Queue=
D E
Print:
F
Visited= A
A B C D E F
1 0 0 0 0 0 B C
Queue=
D E
Print:
F
Visited=
A
A B C D E F
1 0 0 0 0 0 B C
Queue= A
D E
Print:
F
Visited=
A
A B C D E F
1 0 0 0 0 0 B C
Queue=
D E
Print: A
F
Visited=
A
A B C D E F
1 0 0 0 0 0 B C
Queue=
D E
Print: A
F
Visited=
A
A B C D E F
1 1 1 0 0 0 B C
Queue=
D E
Print: A
F
Visited=
A
A B C D E F
1 1 1 0 0 0 B C
Queue= B C
D E
Print: A
F
Visited=
A
A B C D E F
1 1 1 0 0 0 B C
Queue= C
D E
Print: A B
F
Visited=
A
A B C D E F
1 1 1 0 0 0 B C
Queue= C
D E
Print: A B
F
Visited=
A
A B C D E F
1 1 1 1 1 0 B C
Queue= C D E
D E
Print: A B
F
Visited=
A
A B C D E F
1 1 1 1 1 0 B C
Queue= D E
D E
Print: A B C
F
Visited=
A
A B C D E F
1 1 1 1 1 0 B C
Queue= D E
D E
Print: A B C
F
Visited=
A
A B C D E F
1 1 1 1 1 0 B C
Queue= E
D E
Print: A B C D
F
Visited=
A
A B C D E F
1 1 1 1 1 0 B C
Queue= E
D E
Print: A B C D
F
Visited=
A
A B C D E F
1 1 1 1 1 1 B C
Queue= E F
D E
Print: A B C D
F
Visited=
A
A B C D E F
1 1 1 1 1 1 B C
Queue= F
D E
Print: A B C D E
F
Visited=
A
A B C D E F
1 1 1 1 1 1 B C
Queue= F
D E
Print: A B C D E
F
Visited=
A
A B C D E F
1 1 1 1 1 1 B C
Queue= F
D E
Print: A B C D E
F
Visited=
A
A B C D E F
1 1 1 1 1 1 B C
Queue=
D E
Print: A B C D E F
F
Breadth First on Tree
V={0,1,2,3,4,5}
0
E={01,02,13,14,24,34,35,45}
1 2
3 4
5
Depth First Traversal
Depth First Traversal (DFT) algorithm traverses a graph in
a depthward motion and uses a stack to remember to get
the next vertex to start a search.
depthward motion A
B C
D E
F
Visited=
A
A B C D E F
0 0 0 0 0 0
B C
Stack=
D E
Print:
F
Visited=
A
A B C D E F
0 0 0 0 0 0
B C
Stack=
D E
Print:
F
Visited=
A
A B C D E F
1 0 0 0 0 0
B C
Stack=
D E
Print:
A
F
Visited=
A
A B C D E F
1 0 0 0 0 0
B C
Stack=
D E
Print: A
A
F
Visited=
A
A B C D E F
1 0 0 0 0 0
B C
Stack=
D E
Print: A
A
F
Visited=
A
A B C D E F
1 1 0 0 0 0
B C
Stack=
D E
Print: A
B
A
F
Visited=
A
A B C D E F
1 1 0 0 0 0
B C
Stack=
D E
Print: A B
B
A
F
Visited=
A
A B C D E F
1 1 0 1 0 0
B C
Stack=
D E
Print: A B D
B
A
F
Visited=
A
A B C D E F
1 1 0 1 0 0
B C
Stack=
D E
Print: A B D D
B
A
F
Visited=
A
A B C D E F
1 1 0 1 1 0
B C
Stack=
E D E
Print: A B D E D
B
A
F
Visited=
A
A B C D E F
1 1 0 1 1 1
B C
Stack=
F
E D E
Print: A B D E D
B
A
F
Visited=
A
A B C D E F
1 1 0 1 1 1
B C
Stack=
E D E
Print: A B D E F D
B
A
F
Visited=
A
A B C D E F
1 1 0 1 1 1
B C
Stack=
E D E
Print: A B D E F D
B
A
F
Visited=
A
A B C D E F
1 1 1 1 1 1
B C
Stack=
E D E
Print: A B D E F D
B
A
F
Visited=
A
A B C D E F
1 1 1 1 1 1
B C
Stack=
C
E D E
Print: A B D E F D
B
A
F
Visited=
A
A B C D E F
1 1 1 1 1 1
B C
Stack=
C
E D E
Print: A B D E F C D
B
A
F
Visited=
A
A B C D E F
1 1 1 1 1 1
B C
Stack=
E D E
Print: A B D E F C D
B
A
F
Visited=
A
A B C D E F
1 1 1 1 1 1
B C
Stack=
D E
Print: A B D E F C D
B
A
F
Visited=
A
A B C D E F
1 1 1 1 1 1
B C
Stack=
D E
Print: A B D E F C
B
A
F
Visited=
A
A B C D E F
1 1 1 1 1 1
B C
Stack=
D E
Print: A B D E F C
A
F
Visited=
A
A B C D E F
1 1 1 1 1 1
B C
Stack=
D E
Print: A B D E F C
F
Depth First on tree
V={0,1,2,3,4,5}
0
E={01,02,13,14,24,34,35,45}
1 2
3 4
5
Matrix/List Representation of Graph
Two most commonly used representations of a graph are.
1. Adjacency Matrix
2. Adjacency List
An adjacency matrix is defined as follows: Let G be a graph with "n" vertices that are assumed to be
ordered from v1 to vn.The n x n matrix A, in which
aij= 1 if there exists a path from vi to vj
aij = 0 otherwise
is called an adjacency matrix.
Adjacency List is the Array[] of Linked List, where array size is same as number of Vertices in the
graph.
Adjacency Matrix
• Adjacency Matrix is a 2D array of size V x V where V is the number of
vertices in a graph.
• Let the 2D array be adj[][], a slot adj[i][j] = 1 indicates that there is an edge
from vertex i to vertex j.
• Adjacency matrix for undirected graph is always symmetric. Adjacency
matrix is also used to represent weighted graphs.
• If adj[i][j] = w, then there is an edge from vertex i to vertex j with weight w.
Adjacency Matrix
• Consider the following directed graph G (in which the vertices are ordered as
v1, v2, v3, v4, and v5), and its equivalent adjacency matrix representation
v1 v2 v3 v4 v5
v1 0 1 0 1 1
v2 0 0 0 1 0
v3 0 0 0 0 1
v4 0 0 0 0 0
v5 0 1 0 0 0
Adjacency List
• Every Vertex has a Linked List. Each Node in this Linked list represents the
reference to the other vertices which share an edge with the current vertex.
The weights can also be stored in the Linked List Node.
Thank you