Dsa Unit - 4
Dsa Unit - 4
Graph is a non-linear data structure consisting of vertices and edges. The vertices are
sometimes also referred to as nodes and the edges are lines or arcs that connect any
two nodes in the graph. More formally a Graph is composed of a set of vertices( V )
and a set of edges( E ). The graph is denoted by G(V, E).
Vertex 3 has an unvisited adjacent vertex in 5, so we add that to the back of the queue and
visit 4, which is at the front of the queue.
Only 5 remain in the queue since the only adjacent node of 4 i.e. 1 is already visited. We
visited it.
Since the queue is empty, we have completed the Breadth First Traversal of the graph.
Algorithm
void Graph::BFS(int s)
{
bool *visited = new bool[V];
for (int i = 0; i < V; i++)
visited[i] = false;
queue<int> queue;
visited[s] = true;
queue.push(s);
list<int>::iterator i;
while (!queue.empty())
{
s = queue.front();
cout<< s << " ";
queue.pop();
for (i = adj[s].begin(); i != adj[s].end(); ++i)
{
if (!visited[*i]) {
visited[*i] = true;
queue.push(*i);
}
}
}
}
Depth-first search(DFS)
We start from vertex 1, the DFS algorithm starts by putting it in the Visited list and
putting all its adjacent vertices in the stack.
Next, we visit the element at the top of stack i.e. 2 and go to its adjacent nodes. Since
1 has already been visited, we visit 3 instead.
Vertex 3 has an unvisited adjacent vertex in 5, so we add that to the top of the stack and
visit it.
After we visit the last element 4, it doesn't have any unvisited adjacent nodes, so we have
completed the Depth First Traversal of the graph.
Algorithm
void Graph::DFSUtil(int v, bool visited[]) {
visited[v] = true;
cout << v << " ";
for (list<int>::iterator i = adj[v].begin(); i != adj[v].end(); ++i)
if (!visited[*i])
DFSUtil(*i, visited);
}
void Graph::DFS(int s) {
bool *visited = new bool[V];
for (int i = 0; i < V; i++)
visited[i] = false;
DFSUtil(s, visited);
}
Representations of Graph
Here are the two most common ways to represent a graph :
1. Adjacency Matrix
2. Adjacency List
Adjacency Matrix
An adjacency matrix is a way of representing a graph as a matrix of boolean (0’s and
1’s).
Let’s assume there are n vertices in the graph So, create a 2D
matrix adjMat[n][n] having dimension n x n.
Thee below figure shows an undirected graph. Initially, the entire Matrix is initialized
to 0.. If there is an edge from source to destination, we insert 1 to both cases
(adjMat[destination] and adjMat
adjMat[destination]) because we can go either way.
The below figure shows a directed graph. Initially, the entire Matrix is initialized
to 0.. If there is an edge from source to destination, we insert 1 for that
particular adjMat[destination]
adjMat[destination].
An adjacency list is a data structure used to represent a graph where each node in
the graph stores a list of its neighboring vertices.
The below undirected graph has 3 vertices. So, an array of list will be created of size
3, where each indices represent the vertices. Now, vertex 0 has two neighbours (i.e,
1 and 2). So, insert vertex 1 and 2 at indices 0 of array. Similarly, For vertex
verte 1, it has
two neighbour (i.e, 2 and 0) So, insert vertices 2 and 0 at indices 1 of array.
Similarly, for vertex 2, insert its neighbours in array of list.
Representation of Directed Graph to Adjacency list:
The below directed graph has 3 vertices. So, an array of list will be created of size 3,
where each indices represent the vertices. Now, vertex 0 has no neighbours. For
vertex 1, it has two neighbour (i.e, 0 and 2) So, insert vertices 0 and 2 at indices 1 of
array. Similarly, for vertex 2, insert its neighbours in array of list.
Types of Graphs:
6. Bipartite Graphs: A graph in which the vertices can be divided into two
disjoint sets such that every edge connects a vertex in one set to a vertex in the
other set. Example: A job applicant graph where the vertices can be divided into
job applicants and job openings.
7. Trees:: A connected graph with no cycles. Example: A family tree where each
person is connected to their pare
parents.
8. Cycles: A graph with at least one cycle. Example: A bike-sharing graph
where the cycles represent the routes that the bikes take.
9. Sparse Graphs: A graph with relatively few edges compared to the number
of vertices. Example: A chemical reaction graph where each vertex represents a
chemical compound and each edge represents a reaction between two
compounds.
10. Dense Graphs: A graph with many edges compared to the number of
vertices. Example: A social network graph where each vertex represents a person
and each edge represents a friendship.
Finite Graphs
A graph is said to be finite if it has a finite number of vertices and a finite number of
edges. In other words, both the number of vertices and the number of edges in a finite
graph are limited and can be counted.
Infinite Graph:
Trivial Graph:
A graph is said to be trivial if a finite graph contains only one vertex and no edge. It is
also known as a singleton graph or a single vertex graph. A trivial graph is the
simplest type of graph and is often used as a starting point for building more complex
graphs.
Simple Graph
A graph is said to be a simple graph if the graph doesn't consist of no self-loops and no
parallel edges in the graph.
Multi Graph
A graph is said to be a multigraph if the graph doesn't consist of any self-loops, but
parallel edges are present in the graph. If there is more than one edge present between
two vertices, then that pair of vertices is said to be having parallel edges.
Pseudo Graph
If a graph consists of no parallel edges, but self-loops are present in a graph, it is called
a pseudo graph. The meaning of a self-loop is that there is an edge present in the graph
that starts from one of the graph's vertices, and if that edge ends on the same vertex,
then it is called a pseudo graph.
6. Null Graph:
A graph of order n and size zero is a graph where there are only isolated vertices with
no edges connecting any pair of vertices. A null graph is a graph with no edges. In
other words, it is a graph with only vertices and no connections between them. A null
graph can also be referred to as an edgeless graph, an isolated graph, or a discrete
graph
Advantages of graphs:
1. Graphs can be used to model and analyze complex systems and relationships.
2. They are useful for visualizing and understanding data.
3. Graph algorithms are widely used in computer science and other fields, such as
social network analysis, logistics, and transportation.
4. Graphs can be used to represent a wide range of data types, including social
networks, road networks, and the internet.
Disadvantages of graphs: