29 - Data Structure - Graph Data Structure
29 - Data Structure - Graph Data Structure
Structure
The primary operations of a graph include creating a graph with vertices and
edges, and displaying the said graph. However, one of the most common and
popular operation performed using graphs are Traversal, i.e. visiting every
vertex of the graph in a specific order.
Depth First Search is a traversal algorithm that visits all the vertices of a graph
in the decreasing order of its depth. In this algorithm, an arbitrary node is
chosen as the starting point and the graph is traversed back and forth by
marking unvisited adjacent nodes until all the vertices are marked.
The DFS traversal uses the stack data structure to keep track of the unvisited
nodes.
Breadth First Search Traversal
Breadth First Search is a traversal algorithm that visits all the vertices of a
graph present at one level of the depth before moving to the next level of
depth. In this algorithm, an arbitrary node is chosen as the starting point and
the graph is traversed by visiting the adjacent vertices on the same depth level
and marking them until there is no vertex left.
The DFS traversal uses the queue data structure to keep track of the unvisited
nodes.
Representation of Graphs
Adjacency Matrix
Adjacency List
Adjacency Matrix
The Adjacency Matrix is a V×V matrix where the values are filled with either 0
or 1. If the link exists between Vi and Vj, it is recorded 1; otherwise, 0.
Adjacency List
The adjacency list is a list of the vertices directly connected to the other
vertices in the graph.
The adjacency list is −
Types of graph
Directed Graph
Undirected Graph
A directed graph, also called a digraph, is a graph in which the edges have a
direction. This is usually indicated with an arrow on the edge; more formally, if
v and w are vertices, an edge is an unordered pair {v,w}, while a directed edge,
called an arc, is an ordered pair (v,w) or (w,v).
Undirected Graph
Spanning Tree
Properties
Example
there are n number of vertices, the spanning tree should have 𝒏−𝟏 number of
As we have discussed, one graph may have more than one spanning tree. If
edges. In this context, if each edge of the graph is associated with a weight
and there exists more than one spanning tree, we need to find the minimum
spanning tree of the graph.
Moreover, if there exist any duplicate weighted edges, the graph may have
multiple minimum spanning tree.
In the above graph, we have shown a spanning tree though it’s not the
minimum spanning tree. The cost of this spanning tree
is (5+7+3+3+5+8+3+4)=38.
Shortest Path
The shortest path in a graph is defined as the minimum cost route from one
vertex to another. This is most commonly seen in weighted directed graphs
but are also applicable to undirected graphs.
Example
#include <stdio.h>
#include<stdlib.h>
#include <stdlib.h>
#define V 5
struct graph {
};
struct vertex {
// declaring vertices
int end;
};
struct Edge {
// declaring edges
};
int i;
graph->point[i] = NULL;
v->end = end;
v->next = graph->point[start];
graph->point[start] = v;
return graph;
}
struct Edge edges[] = { {0, 1}, {0, 2}, {0, 3}, {1, 2}, {1, 4}, {2, 4}, {2, 3}, {3, 1} };
int i;
ptr = ptr->next;
printf ("\n");
return 0;
Output
(1 -> 3) (1 -> 0)
(2 -> 1) (2 -> 0)
(3 -> 2) (3 -> 0)
(4 -> 2) (4 -> 1)