Data structures can be broadly classified into two types:
A graph is a non-linear data structure consisting of nodes that have data and are connected to other nodes through edges. Graphs have a wide-ranging application in real life. These include analysis of electrical circuits, finding the shortest routes between two places, building navigation systems like Google Maps, even social media using graphs to store data about each user, etc.
There are two main components of graphs:
Node(Vertex):
Note: An undirected graph can be converted to a directed graph by converting all the undirected edges into directed edges. An undirected edge between two nodes u and v can be represented as pair of two directed edges, one from node u to v, and other from node v to u as shown in the figure:
Consider the following undirected graph:
It is not necessary that all the nodes in the graph are connected to each other. Here, comes the concept of a component in the graph. A component of a graph is a group of nodes which are connected to each other directly or indirectly and which are not connected any node from any other group.
Example: Consider the following graph:
This graph contains 4 components- {1, 2, 3, 4}, {5, 6, 7}, {8, 9} and {10}.
A path in a graph is a sequence of vertices where each adjacent pair of vertices is connected by an edge. A path always contain unique nodes, i.e., a node cannot appear twice in a path.
It can be of two types:
1 2 3 5
. 1 2 3 2 1
is not a path, because a node can't appear twice in a path.1 3 5
is not a path, as adjacent nodes must have an edge and there is no edge between 1 and 3.
The degree of a node in a graph is a measure of the number of edges connected to that node.
Total degree of graph = 2+2+3+2+3 = 2 X E
(where E = 6)
Some graphs have edges that have weights associated to them. It is often referred to as the cost of the edge.
Based on whether the edges in graph are weighted or unweighted, the graphs can be divided into two types:
Weighted Graphs: Having edges that have weights associated with them.
Unweighted Graphs: Having edges that do not have weights associated with them.
Note: In applications, weight may be a measure of the cost of a route. For example, if vertices A and B represent towns in a road network, then weight on edge AB may represent the cost of moving from A to B, or vice versa.
In the question, it will be mentioned whether the graph is directed or undirected.
The first hurdle while solving any graph problem is how to represent the graphs in the programming languages. The two most commonly used representations for graphs are:
a[i][j] = 1
if the edge (vᵢ, vⱼ) is in the set of edges, and a[i][j] = 0
if there is no such edge.
Input:
5 6
1 2
1 3
2 4
3 4
3 5
4 5
Explanation:
Number of nodes, n = 5
Number of edges, m = 6
Next m lines represent the edges.
#include <bits/stdc++.h>
using namespace std;
int main() {
// Taking the input
int n, m;
cin >> n >> m;
// Adjacency matrix to store the graph
int adj[n+1][n+1];
// Add all the edges in the graph
for(int i = 0; i < m; i++) {
// Taking the input
int u, v;
cin >> u >> v;
// Adding the edges
adj[u][v] = 1;
/* This statement will be removed
in the case of directed graph */
adj[v][u] = 1;
}
return 0;
}
#include <bits/stdc++.h>
using namespace std;
int main() {
// Taking the input
int n, m;
cin >> n >> m;
// adjacency list for undirected graph
vector<int> adj[n+1];
// Add the edges to the list
for(int i = 0; i < m; i++) {
// Taking the input
int u, v;
cin >> u >> v;
// Adding the edges
adj[u].push_back(v);
adj[v].push_back(u);
}
return 0;
}
Consider the following undirected weighted graph:
In the question, it will be mentioned whether the graph is directed or undirected.
The first hurdle while solving any graph problem is how to represent the graphs in the programming languages. The two most commonly used representations for graphs are:
a[i][j] = 1
if the edge (vᵢ, vⱼ) is in the set of edges, and a[i][j] = 0
if there is no such edge.
Input:
5 6
1 2
1 3
2 4
3 4
3 5
4 5
Explanation:
Number of nodes, n = 5
Number of edges, m = 6
Next m lines represent the edges.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// Taking the input
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
// adjacency matrix for undirected graph
int[][] adj = new int[n + 1][n + 1];
// Add the edges to the matrix
for (int i = 0; i < m; i++) {
// Taking the input
int u = sc.nextInt();
int v = sc.nextInt();
// Adding the edges
adj[u][v] = 1;
adj[v][u] = 1;
}
sc.close();
}
}
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// Taking the input
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
// adjacency list for undirected graph
ArrayList<ArrayList<Integer>> adj = new ArrayList<>(n + 1);
// Initialize the adjacency list
for (int i = 0; i <= n; i++) {
adj.add(new ArrayList<>());
}
// Add the edges to the list
for (int i = 0; i < m; i++) {
// Taking the input
int u = sc.nextInt();
int v = sc.nextInt();
// Adding the edges
adj.get(u).add(v);
adj.get(v).add(u);
}
sc.close();
}
}
Consider the following undirected weighted graph: