Unit 3 Dsa Graph 2022 1
Unit 3 Dsa Graph 2022 1
SYLLABUS
Basic Concepts, Storage representation, Adjacency matrix,
Closed Path
A path will be called as closed path if the initial
node is same as terminal node. A path will be
closed path if V0=VN.
Simple Path
If all the nodes of the graph are distinct with an
Terminologies of
Cycle
Graph
A cycle can be definedas the path which
has no repeated edges or vertices except the first
and last vertices.
Connected Graph
A connected graph is the one in which some path
exists between every two vertices (u, v) in V. There
are no isolated nodes in connected graph.
Terminologies of
Graph
Complete Graph
A complete graph is the one in which every node is
connected with all other nodes. A complete graph
contain n(n-1)/2 edges where n is the number of
nodes in the graph.
Terminologies of
Weighted Graph
Graph
In a weighted graph, each edge is assigned with
some data such as length or weight. The weight
of an edge e can be given as w(e) which must be
a positive (+) value indicating the cost of
traversing the edge.
Terminologies of
Weighted Graph
Graph
In a weighted graph, each edge is assigned with
some data such as length or weight. The weight
of an edge e can be given as w(e) which must be
a positive (+) value indicating the cost of
traversing the edge.
Digraph
A digraph is a directed graph in which each edge of
the graph is associated with some direction and the
traversing can be done only in the specified
Terminologies of
Loop
Graph
An edge that is associated with the similar end points
can be called as Loop.
Adjacent Nodes
If two nodes u and v are connected via an edge e, then
the nodes u and v are called as neighbours or adjacent
nodes.
Adjacent Nodes
If two nodes u and v are connected via an edge e, then
the nodes u and v are called as neighbours or adjacent
nodes.
the origin.
Isolated Vertex
6. is empty(Graph) :Boolean;
7. end graph 20
Graph
Representation
Adjacency Matrix Representation of Graph
We can easily represent the graphs using the
following ways,
3. Adjacency Multilist
in the above figure, we can see the mapping among the vertices (A, B, C, D, E)
is represented by using the adjacency matrix which is also shown in the figure.
There exists different adjacency matrices for the directed and undirected graph.
In directed graph, an entry Aij will be 1 only when there is an edge directed from Vi to
Graph
Representation
An undirected graph and its
adjacency representation is shown in
matri
x
the following figure.
Graph
Representation
A directed graph and its adjacency matrix representation is shown in
the following figure.
Consider the directed graph shown in the following figure and check
the adjacency list representation of the graph.
Graph
Representation
43
The node structure of such a list can be represented as
follows :
44
45
Inverse adjacency lists is a set of lists that
contain one list for vertex
46
47
Graph
Representation
Degree of Vertex in
DegreeGraph
of Vertex in a Directed Graph
Indegree of a Graph
Indegree of vertex V is the number of edges which are coming into
the vertex V.
Notation − deg−(V).
Outdegree of a Graph
Outdegree of vertex V is the number of edges which are going out
from the vertex V.
Degree of Vertex in
Graph
In-degree : Out-degree
In-degree of a vertex is the number of Out-degree of a vertex is the number edges
edges coming to the vertex. which are coming out from the vertex.
Step 2: Enqueue the starting node A and set its STATUS = 2 (waiting state)
Step 5: Enqueue all the neighbours of N that are in the ready state
(whose STATUS = 1) and set
their STATUS = 2
(waiting state)
[END OF LOOP]
Example of BFS
Algorithms
Complexity of BFS
Algorithms
The time complexity of the BFS algorithm is
• Disadvantages :
A BFS on a binary tree generally requires more memory than a DFS.
Application of BFS
1. Algorithms
BFS can be used to find the neighboring locations from a given source
location.
3. BFS is used to determine the shortest path and minimum spanning tree.
The data structure which is being used in DFS is stack. The process
is similar to BFS algorithm. In DFS, the edges that leads to an unvisited
node are called discovery edges while the edges that leads to an already
visited node are called block edges.
DFS
Depth First Search (DFS) algorithm traverses a
Algorithms
graph in a depthward motion and uses a stack to
remember to get the next vertex to start a search,
when a dead end occurs in any iteration.
•As in the example given above, DFS algorithm
traverses from A to B to C to D first then to E,
then to F and lastly to G. It employs the following
rules.
•Rule 1 − Visit the adjacent unvisited vertex.
Mark it as visited. Display it. Push it in a stack.
Traversing the above graph in BFS fashion would result from A -> B -> E -
> F -> C -> G -> D. The algorithm starts from node A and traverses all its
child nodes. As soon as it encounters B, it seems that it has further child
nodes. So, the child nodes of B are traversed before proceeding to the next
child node of A
DFS
Step 1: SET STATUS = 1 (ready state) for each node in G
Algorithms
Step 2: Push the starting node A on the stack and set its STATUS = 2 (waiting
state)
Step 4: Pop the top node N. Process it and set its STATUS = 3 (processed state)
[END OF LOOP]
Step 6: EXIT
DFS Algorithms
Depth first search (DFS) algorithm starts with the initial node of the
graph G, and then goes to deeper and deeper until we find the goal
node or the node which has no children. The algorithm, then
backtracks from the dead end towards the most recent node that is
yet to be completely unexplored.
The data structure which is being used in DFS is stack. The process
is similar to BFS algorithm. In DFS, the edges that leads to an
unvisited node are called discovery edges while the edges that leads
to an already visited node are called block edges.
DFS Algorithms
DFS is an algorithm for finding or traversing graphs or trees
in depth-ward direction. The execution of the algorithm
begins at the root node and explores each branch before
backtracking. It uses a stack data structure to remember, to
get the subsequent vertex, and to start a search, whenever a
dead-end appears in any iteration.
Step 2: Push the starting node A on the stack and set its STATUS = 2
(waiting state)
Step 4: Pop the top node N. Process it and set its STATUS = 3 (processed
state)
Step 5: Push on the stack all the neighbours of N that are in the ready
state (whose STATUS = 1) and set their
STATUS = 2 (waiting state)
[END OF LOOP]
Step 6: EXIT
DFS Algorithms Example
• Disadvantages :
A DFS doesn't necessarily find the shortest path to a node, while
breadth- first search does.
Application of DFS Algorithms
1. For finding the path
graph
You can never be trapped into You can be trapped into infinite
8
finite loops. loops.
2. Cluster Analysis
Algorithm Steps :
2. Start adding edges to the MST from the edge with the smallest weight
until the edge of the largest weight.
3. Only add edges which doesn't form a cycle , edges which connect
only disconnected components.
GREEDY
STRATEGIES
1. Kruskal’s algorithms :
•
Kruskal's algorithm is a minimum spanning tree algorithm
that takes a graph as input and finds the subset of the edges of
that graph which
• has the minimum sum of weights among all the trees that can
be formed from the graph
• Kruskal's Algorithm Complexity
• The time complexity Of Kruskal's Algorithm is: O(E log E).
GREEDY
STRATEGIES
1. Kruskal’s algorithms :
The steps for implementing Kruskal's algorithm are
as follows:
2. Take the edge with the lowest weight and add it to the
spanning tree. If adding the edge created a cycle, then
reject this edge.
.
GREEDY
STRATEGIES
2. Kruskal’s algorithms : Example
.
1
GREEDY
STRATEGIES
2. Kruskal’s algorithms : Example
.
GREEDY
STRATEGIES
2. Kruskal Algorithms : Example
GREEDY
STRATEGIES
1. Kruskal Algorithms : Example
GREEDY
STRATEGIES
1. Kruskal Algorithms :
Algorithm Steps:
2.Find all the edges that connect the tree to new vertices, find the
minimum and add it to the tree.
.
1
GREEDY
STRATEGIES
2. Prims algorithm: Example
GREEDY
STRATEGIES
2. Prims algorithm: Example
GREEDY
STRATEGIES
2. Prims algorithm: Example
GREEDY
STRATEGIES
2. Prims algorithm: Example
GREEDY
STRATEGIES
2. Prims algorithm: Example
Kruska's algorithm.
GREEDY
STRATEGIES
2. Prims algorithm:
Disadvantages :
• The greedy doesn't always produce the optimal
algorithm solution.
Applications of Greedy
Algorithms
• It is used in finding the shortest path.
https://www.freecodecamp.org/news/dijkstras-shortest-path-algorithm-
visual-introduction/
Exampleof Dijkstra’s
Algorithms
ANSWERs : https://www.freecodecamp.org/news/dijkstras-shortest-path-
algorithm-visual-introduction/
Exampleof Dijkstra’s
Algorithms
Time Complexity Dijkstra’s
Algorithms
• Time Complexity: O(E Log V)
• In a telephone network
Shorst
Floyd-Warshall
• Algorithms
Floyd Warshall Algorithm is a famous algorithm.
for k = 1 to n
for i = 1 to n
for j = 1 to n
Ak[i, j] =
min (Ak-
1[i, j],
Ak-1[i, k]
+ Ak-1[k,
Example of Floyd-Warshall
Algorithms
Initial graph
Example : https://www.programiz.com/dsa/floyd-warshall-algorithm
Example of Floyd-Warshall
Algorithms
Fill each cell with the distance between ith and jth vertex
Example of Floyd-Warshall
Algorithms
1. It is extremely simple.
2. It is easy to implement.
Floyd-Warshall
• Algorithms
Time Complexity
There are three loops. Each loop has constant complexities.
So, the time complexity of the Floyd-Warshall algorithm is
3
O(n ).
• Space Complexity
The space complexity of the Floyd-Warshall algorithm
2
is O(n ).
Applications of Floyd-
Warshall
Algorithms
1. To find the shortest path is a directed graph
Step-01:
In case-02,
3. Remove vertex-D and its associated edges.
4. Then, update the in-degree of other vertices.
Topological
Step-04:Sort/Ordering
Topological
Sort/Ordering
Conclusion-
ABCDE
ABDC
E
Topological
Sort/Ordering
2. Instruction Scheduling
4. Data Serialization