CA229 Unit 05
CA229 Unit 05
B.C.A. (Semester-3)
CA229 || Fundamentals of Data Structures and Algorithms
An example of a real-life situation that can be modeled by a graph is the airport system.
Each airport is a vertex and
Two vertices have connected an edge if there is a nonstop flight from the
airports that are represented by the vertices.
The edge could have weight, representing the time, distance, or cost of the flight.
Definition:
Graph:
Graph G consists of a nonempty set V called the setoff nodes (points, vertices) of the
graph, a set E which is the set of edges of the graph, and a mapping from the set E to a
set V.
A vertex (Basic Element) is simply drawn as a node or a dot. The vertex set of G is usually
denoted by V(G). The order of a graph is the number of its vertices, i.e. |V(G)|. And edge (a
set of two elements) is drawn as a line connecting two vertices, called end vertices or
endpoints. An edge with end vertices x and y is denoted by xy (without any symbol in
between).The edge set of G is usually denoted by |E(G)|.
The size of a graph is the number of its edge, i.e. |E(G)|.
1
3
2
5
4
Graph G is defined as a set of two tuples. i.e. G = {V, E} where, V = set of vertices. E =
set of edges.
1|Page
Direct Edge:
An edge for which direction is specified is called
Directed Edge.
Undirected Edge:
An edge for which direction is not specified is
called Undirected Edge.
Directed Graph (Digraph):
A graph in which every edge is directed is called
a directed graph or digraph.
Undirected Graph:
A graph in which every edge is undirected is
called an undirected graph.
Mix Graph:
A graph that contains some directed edges and
some undirected edges is called Mix Graph.
E3
Adjacent node:
e1
Any two nodes which are connected by an edge
it is called Adjacent Node.
2|Page
Loop/Sling:
If a node is connected t o itself using any edge
is known as LOOP or SLING.
Parallel Edge:
If two nodes are connected using more than one
edge then the such edge is known as Parallel
Edge,
Multi Graph:
A graph that contains more than one parallel
edge is called Multi Graph.
Simple Graph:
A C
A graph in which nodes are connected using one
edge is called a Simple Graph.
B D
Weighted Graph:
5
A graph in which a value for an edge is given is A C
B D
6
3|Page
Isolated Node:
A node that is not connected or adjacent to
another node is an Isolated node.
Here, D is an Isolated node.
Null Graph:
A B
A graph containing only an isolated node is
called Null Graph. It means it does not have any
edge.
C D
Indegree:
The in-degree of vertex V in the directed graph
is the no. of edges ending as it, which is the
Indegree of a graph.
Indegree of A = 0
Indegree of B = 2
Indegree of C = 2
Indegree of D = 2
OutDegree:
The Outdegree of vertex V in a directed graph
is no. of edges beginning fromV.
Outdegree of A = 3
Outdegree of B = 1
Outdegree of C = 1
Outdegree of D = 1
4|Page
Total Degree:
The total degree of a graph is a sum of indegree
and outdegree.
Total Degree = 12
(Indegree = 6)
(Outdegree = 6)
Path:
A route that does not pass any edge more than
once, is known as PATH.That means traversing
the route of the node appearing in sequence
starting nodeto the ending node.
Path is A – B – C – D
Simple Path:
A path in which edges are distinct is known as
a Simple Path.
Path is A – B – C - D
5|Page
Cycle:
A graph same starting and ending row is called
Cycle.
Acyclic Graph:
A graph, which does not have any cycle is called
Acyclic Graph.
First Path: A – B – C
Second Path: A – B – D
Third Path: A - C
Elementary Path:
A path in which nodes are distinct or different
is called Elementary Path.
Elementary Path: A – B – C - A
Traversing a graph is different than traversing a tree because there is no first node or
root node in a graph. So, traversal can start from any node. Also in a tree, all nodes are
reachable from the root node, so traversing all nodes is easier. While in the graph, it is
possible that some nodes are not reachable from the start node, so we have to recursively
6|Page
check whether all nodes are visited or not.
There are two methods of traversing a graph.
1. Breadth-First Search.
2. Depth First Search.
Breadth-First Search:
This method is used to traverse a graph for finding the shortest path from the starting
node to the ending node. Using the BFS algorithm, the shortest distance is the minimum
number of edges from the start node to the level that is the first level, second level, etc.
In BFS we start from any node of the graph which will be the starting node of traversal.
First, we visit all the adjacent nodes of that start node. Then we take the next start node
whichever is the nearest to the main start node. Again, we follow the same process which
is visiting all adjacent nodes. Here we maintain the flag which will monitor that one node
should not visit more than once.
We can find the BFS of the following graph using a queue as follow.
7|Page
Example 1:
Step 7: Step 8:
ABDCFE ABDCFEG
G
8|Page
Applications of BFS:
1. For GPS navigation
2. Path finding algorithms
3. In Ford-Fulkerson algorithm to find maximum flow in a network.
4. Cycle detection in an undirected graph.
5. In minimum spanning tree.
If Node0 has many adjacent nodes, then any node will be selected for visit. As it is visited
it will be marked as TRUE (visited), so then it will not besearched again. This process
is continued until the current path ends at anode without degree zero or a node with all
adjacent nodes already marked. Then the search returns to the last node which still has
unvisited adjacent nodes and continues the process.
Algorithm of DFS:
Step 1: Initialize all nodes to a ready state.
Step 2: Push the starting node “Node0” onto the stack and change its state
from the ready state to the waiting state.
Step 3: Repeat steps 4 and 5 until the stack is empty.
Step 4: Pop the topmost element of the stack and change its state from the
waiting state to the process state.
Step 5: Push all neighbors of the deleted node into the stack.
Step 6: Exit
We can find the DFS of the following graph using the stack as follows.
9|Page
Example 1:
D H
C A C AD
A B B
J
C ADH C ADHJ F ADHJC
B B B
10 | P a g e
Application of DFS Algorithm:
1. For finding the path
2. To test if the graph is bipartite
3. For finding the strongly connected components of a graph
4. For detecting cycles in a graph.
A B C
A 0 1 0
B 0 0 1
C 0 0 0
11 | P a g e
Example 2:
A B C D E F G H I J K
A 0 1 1 1 0 0 0 0 0 0 0
B 0 0 0 0 1 0 0 0 0 0 0
C 0 0 0 0 0 1 0 0 0 0 0
D 0 0 0 0 0 0 1 0 0 0 0
E 0 0 0 0 0 1 0 1 0 0 0
F 0 0 0 0 0 0 0 0 0 0 0
G 0 0 0 0 0 1 0 0 0 1 0
H 0 0 0 0 0 0 0 0 1 0 1
I 0 0 0 0 0 0 0 0 0 0 1
J 0 0 0 0 0 0 0 0 1 0 1
K 0 0 0 0 0 0 0 0 0 0 0
12 | P a g e
The biggest advantage, however, comes from the use of matrices. The recent
advances in hardware enable us to perform even expensive matrix operations on
the GPU.
By performing operations on the adjacent matrix, we can get important insights
into the nature of the graph and the relationship between its vertices.
Cons of Adjacency Matrix
The VxV space requirement of the adjacency matrix makes it a memory hog.
Graphs out in the wild usually don't have too many connections and this is the
major reason why adjacency lists are the better choice for most tasks.
While basic operations are easy, operations like inEdges and outEdges are
expensive when using the adjacency matrix representation.
Path Matrix:
Let G(V, E) be your directed graph with n vertices v1,v2,v3, vn then the pathmatrix of
a graph G is defined as.
aij = 1 if there is a path, exists both vertices vi and vj
=0 otherwise
Example 1:
Example 2:
A B C D E
A 1 1 1 1 1
B 1 1 1 1 1
C 1 1 1 1 1
D 0 0 0 0 0
E 0 0 0 1 0
13 | P a g e
Pros of Adjacency List:
An adjacency list is efficient in terms of storage because we only need to store the
values for the edges. For a sparse graph with millions of vertices and edges, this
can mean a lot of saves space.
It also helps to find all the vertices adjacent toa vertex easily.
Cons of Adjacency List:
Finding the adjacent list is not quicker than the adjacency matrix because all the
connected nodes must be first explored to find them.
Spanning Tree:
What is a Spanning Tree?
Given an undirected and connected graph G = (V, E), a spanning tree of the graph G is a
tree that spans G (that is, it includes every vertex of G) and is a subgraph of G (every
edge in the tree belongs to G)
14 | P a g e
Undirected Graph
There are two famous algorithms for finding the Minimum Spanning Tree(MST).
Kruskal’s Algorithm:
Kruskal’s Algorithm is used to find the minimum spanning tree for a connected weighted
graph. The main target of this algorithm is to find the subset of edges by using which, we
can traverse every vertex of the graph. Kruskal's algorithm follows a greedy approach as
in each iteration it finds an edge that has the least weight and adds it to the growing
spanning tree.
Algorithm:
Step 1: Sort the graph edges concerning their weights.
Step 2: Start adding edges to the MST from the edge with the smallest weight
to the edge with the largest weight.
Step 3: Only add edges that don’t form a cycle, edges that connect only
disconnected components.
Step 4: Exit
15 | P a g e
Example:
Solution:
The weight of the edges is given as:
Edge AE AD AC AB BC CD DE
Weight 5 10 7 1 3 4 2
16 | P a g e
3. Add BC to the MST:
5. The next step is to add AE, but we can't add that as it will cause a cycle.
6. The next edge to be added is AC, but it can't be added as it will cause acycle.
7. The next edge to be added is AD, but it can't be added as it will contain acycle.
Hence, the final MST is the one which is shown in step 4.
Time Complexity:
In Kruskal’s algorithm, the most time-consuming operation is sorting because the total
complexity of the Disjoint-Set operations will be O(ElogV), which is the overall Time
Complexity of the algorithm.
Prim’s Algorithm:
Prim’s Algorithm is used to find the minimum spanning tree from a graph. Prim’s
algorithm finds the subset of edges that includes every vertexof the graph such that
17 | P a g e
the sum of weights of the edges can be minimized. Prim’s algorithm starts with a single
node and explores all the adjacent nodes with all the connecting edges at every step. The
edges with the minimal weights causing no cycles in the graph got selected.
Algorithm:
Step 1: Select a starting vertex
Step 2: Repeat Steps 3 and 4 until there are fringe vertices
Step 3: Select an edge e connecting the tree vertex and fringe vertex
that has a minimum weight
Step 4: Add the edge and the vertex to the minimum selected
spanning tree T [END OF LOOP]
Step 5: Exit
Example:
Solution:
Step 1: Choose a starting vertex B.
Step 2: Add the vertices that are adjacent to B. The edges that connect the vertices
are shown by dotted lines.
18 | P a g e
Step 3: Choose the edge with the minimum weight among all. i.e. BD and add it to
MST. Add the adjacent vertices of D i.e. C and E.
Step 4: Choose the edge with the minimum weight among all. In this case, the edges
DE and CD are such edges. Add them to MST and explore the adjacent of C
i.e. E and A
Step 5: Choose the edge with the minimum weight i.e. CA. We can’t choose CE as it
would cause a cycle in the graph.
19 | P a g e
Step 6: Exit
The graph produces in step 5 is the minimum spanning tree of the graphshown in the
above figure.
The cost of MST will be calculated as;
Cost (MST) = 4 + 2 + 1 + 3
= 10 units.
Time Complexity:
The time complexity of Prim’s Algorithm is O((V+E)logV) because each vertex is inserted
in the priority queue only once and insertion in the priority queue takes logarithmic time.
20 | P a g e