Module V Graph
Module V Graph
A graph is an advanced data structure that is used to organize items in an interconnected network. Each
item in a graph is known as a node(or vertex) and these nodes are connected by edges.
In the figure below, we have a simple graph where there are five nodes in total and six edges.
The nodes in any graph can be referred to as entities and the edges that connect different nodes
define the relationships between these entities. In the above graph we have a set of nodes {V} = {A, B,
C, D,E} and a set of edges, {E} = {A-B, A-D, B-C, B-D, C-D, D-E} respectively
Types of Graphs
1. Null Graphs
2. Undirected Graphs
If we take a look at the pictorial representation that we had in the Real-world example above, we can clearly
see that different nodes are connected by a link (i.e. edge) and that edge doesn't have any kind of direction
associated with it. For example, the edge between "Anuj" and "Deepak" is bi-directional and hence the
relationship between them is two ways, which turns out to be that "Anuj" knows "Deepak" and "Deepak"
also knows about "Anuj". These types of graphs where the relation is bi-directional or there is not a single
direction, are known as Undirected graphs.
3. Directed Graphs
What if the relation between the two people is something like, "Shreya" know "Abhishek" but "Abhishek"
doesn't know "Shreya". This type of relationship is one-way, and it does include a direction. The edges with
arrows basically denote the direction of the relationship and such graphs are known as directed graphs.
It can also be noted that the edge from "Shreya" to "Abhishek" is an outgoing edge for "Shreya" and an
incoming edge for "Abhishek".
4. Cyclic Graph
A graph that contains at least one node that traverses back to itself is known as a cyclic graph. In simple
words, a graph should have at least one cycle formation for it to be called a cyclic graph.
5. Acyclic Graph
A graph where there's no way we can start from one node and can traverse back to the same one, or simply
doesn't have a single cycle is known as an acyclic graph.
6. Weighted Graph
When the edge in a graph has some weight associated with it, we call that graph as a weighted graph. The
weight is generally a number that could mean anything, totally dependent on the relationship between the
nodes of that graph.
7. Connected Graph
A graph where we have a path between every two nodes of the graph is known as a connected graph. A path
here means that we are able to traverse from a node "A" to say any node "B". In simple terms, we can say
that if we start from one node of the graph we will always be able to traverse to all the other nodes of the
graph from that node, hence the connectivity.
8. Disconnected Graph
A graph that is not connected is simply known as a disconnected graph. In a disconnected graph, we will not
be able to find a path from between every two nodes of the graph.
9. Complete Graph
A graph is said to be a complete graph if there exists an edge for every pair of vertices(nodes) of that graph.
A graph is said to be a multigraph if there exist two or more than two edges between any pair of nodes in the
graph.
• Path - A sequence of alternating nodes and edges such that each of the successive nodes are
connected by the edge.
• Cycle - A path where the starting and the ending node is the same.
• Simple Path - A path where we do not encounter a vertex again.
• Bridge - An edge whose removal will simply make the graph disconnected.
• Forest - A forest is a graph without cycles.
• Tree - A connected graph that doesn't have any cycles.
• Degree - The degree in a graph is the number of edges that are incident on a particular node.
• Neighbour - We say vertex "A" and "B" are neighbours if there exists an edge between them.
Weighted Graph will contains weight on each edge where as unweighted does not.
Following is an example, where both graphs looks exactly the same but one is weighted another is not.
What difference does it make ?
Let’s take the same example to find shortest path from A to F. Result is different, just because one is
weighted another doesn’t.
But how?
Because when you doesn’t have weight, all edges are considered equal. Shortest distance means less number
of nodes you travel.
But in case of weighted graph, calculation happens on the sum of weights of the travelled edges.
BFS is an algorithm that is used to graph data or searching tree or traversing structures. The algorithm
efficiently visits and marks all the key nodes in a graph in an accurate breadthwise fashion.
This algorithm selects a single node (initial or source point) in a graph and then visits all the nodes adjacent
to the selected node. Once the algorithm visits and marks the starting node, then it moves towards the
nearest unvisited nodes and analyses them.
Once visited, all nodes are marked. These iterations continue until all the nodes of the graph have been
successfully visited and marked. The full form of BFS is the Breadth-first search.
Steps in Breadth First Search
1. Start by putting any one of the graph’s vertices at the back of a queue.
2. Take the front item of the queue and add it to the visited list.
3. Create a list of that vertex’s adjacent nodes. Add the ones which aren’t in the visited list to the
Example of BFS
Example of BFS
Step 1)
Step 2)
Step 3)
Step 4)
8.
Remaining 0 adjacent and unvisited nodes are visited, marked, and inserted into the queue.
Step 5)
Depth First Search.
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. The full form of DFS is Depth-first search.
Example of DFS
In the following example of DFS, we have used an undirected graph having 5 vertices.
Step 1)
We have started from vertex 0. The algorithm begins by putting it in the visited list and simultaneously
putting all its adjacent vertices in the data structure called stack.
Step 2)
You will visit the element, which is at the top of the stack, for example, 1 and go to its adjacent nodes. It is
because 0 has already been visited. Therefore, we visit vertex 2.
Step 3)
Subject :( CSE 116 -Data Structure) Class :( F. Y. B. Tech CSE SEM II)
Module V- Graph
Vertex 2 has an unvisited nearby vertex in 4. Therefore, we add that in the stack and visit it.
Step 4)
Finally, we will visit the last vertex 3, it doesn't have any unvisited adjoining nodes. We have completed thetraversal of
the graph using DFS algorithm.