Graph Data Structure and Algorithms: Recent Articles On Graph
Graph Data Structure and Algorithms: Recent Articles On Graph
In the above Graph, the set of vertices V = {0,1,2,3,4} and the set of
edges E = {01, 12, 23, 34, 04, 14, 13}.
Graphs are used to solve many real-life problems. Graphs are used to
represent networks. The networks may include paths in a city or
telephone network or circuit network. Graphs are also used in social
networks like linkedIn, Facebook. For example, in Facebook, each person
is represented with a vertex(or node). Each node is a structure and
contains information like person id, name, gender, locale etc.
Adjacency List:
An array of lists is used. Size of the array is equal to the number of
vertices. Let the array be array[]. An entry array[i] represents the list of
vertices adjacent to the ith vertex. This representation can also be used
to represent a weighted graph. The weights of edges can be represented
as lists of pairs. Following is adjacency list representation of the above
graph.
// Java Program to demonstrate adjacency list
// representation of graphs
import java.util.LinkedList;
public class GFG
{
// A user define class to represent a graph.
// A graph is an array of adjacency lists.
// Size of array will be V (number of vertices
// in graph)
static class Graph
{
int V;
LinkedList<Integer> adjListArray[];
// constructor
Graph(int V)
{
this.V = V;
// define the size of array as
// number of vertices
adjListArray = new LinkedList[V];
// Create a new list for each vertex
// such that adjacent nodes can be stored
for(int i = 0; i < V ; i++){
adjListArray[i] = new LinkedList<>();
}
}
}
// Adds an edge to an undirected graph
static void addEdge(Graph graph, int src, int dest)
{
// Add an edge from src to dest.
graph.adjListArray[src].add(dest);
// Since graph is undirected, add an edge from dest
// to src also
graph.adjListArray[dest].add(src);
}
// A utility function to print the adjacency list
// representation of graph
static void printGraph(Graph graph)
{
for(int v = 0; v < graph.V; v++)
{
System.out.println("Adjacency list of vertex "+
v);
System.out.print("head");
for(Integer pCrawl: graph.adjListArray[v]){
System.out.print(" -> "+pCrawl);
}
System.out.println("\n");
}
}
// Driver program to test above functions
public static void main(String args[])
{
// create the graph given in above figure
int V = 5;
Graph graph = new Graph(V);
addEdge(graph, 0, 1);
addEdge(graph, 0, 4);
addEdge(graph, 1, 2);
addEdge(graph, 1, 3);
addEdge(graph, 1, 4);
addEdge(graph, 2, 3);
addEdge(graph, 3, 4);
// print the adjacency list representation of
// the above graph
printGraph(graph);
}
}
Output:
The above code traverses only the vertices reachable from a given
source vertex. All the vertices may not be reachable from a given vertex
(example Disconnected graph). To do complete DFS traversal of such
graphs, we must call DFSUtil() for every vertex. Also, before calling
DFSUtil(), we should check if it is already printed by some other call of
DFSUtil(). Following implementation does the complete graph traversal
even if the nodes are unreachable. The differences from the above code
are highlighted in the below code.
// Java program to print DFS traversal from a given given
graph
import java.io.*;
import java.util.*;
// This class represents a directed graph using adjacency list
// representation
class Graph
{
private int V; // No. of vertices
// Array of lists for Adjacency List Representation
private LinkedList<Integer> adj[];
// Constructor
Graph(int v)
{
V = v;
adj = new LinkedList[v];
for (int i=0; i<v; ++i)
adj[i] = new LinkedList();
}
//Function to add an edge into the graph
void addEdge(int v, int w)
{
adj[v].add(w); // Add w to v's list.
}
// A function used by DFS
void DFSUtil(int v,boolean visited[])
{
// Mark the current node as visited and print it
visited[v] = true;
System.out.print(v+" ");
// Recur for all the vertices adjacent to this vertex
Iterator<Integer> i = adj[v].listIterator();
while (i.hasNext())
{
int n = i.next();
if (!visited[n])
DFSUtil(n,visited);
}
}
// The function to do DFS traversal. It uses recursive
DFSUtil()
void DFS()
{
// Mark all the vertices as not visited(set as
// false by default in java)
boolean visited[] = new boolean[V];
// Call the recursive helper function to print DFS
traversal
// starting from all vertices one by one
for (int i=0; i<V; ++i)
if (visited[i] == false)
DFSUtil(i, visited);
}
public static void main(String args[])
{
Graph g = new Graph(4);
g.addEdge(0, 1);
g.addEdge(0, 2);
g.addEdge(1, 2);
g.addEdge(2, 0);
g.addEdge(2, 3);
g.addEdge(3, 3);
System.out.println("Following is Depth First
Traversal");
g.DFS();
}
}
Output:
Following is Depth First Traversal
0123
Time Complexity: O(V+E) where V is number of vertices in the graph and
E is number of edges in the graph.
7) Solving puzzles with only one solution, such as mazes. (DFS can be
adapted to find all solutions to a maze by only including nodes on the
current path in the visited set.)
Graph Cycle :
Detect Cycle in a Directed Graph
Given a directed graph, check whether the graph contains a cycle or not.
Your function should return true if the given graph contains at least one
cycle, else return false. For example, the following graph contains three
cycles 0->2->0, 0->1->2->0 and 3->3, so your function must return true.
Depth First Traversal can be used to detect a cycle in a Graph. DFS for a
connected graph produces a tree. There is a cycle in a graph only if there
is a back edge present in the graph. A back edge is an edge that is from a
node to itself (self-loop) or one of its ancestor in the tree produced by
DFS. In the following graph, there are 3 back edges, marked with a cross
sign. We can observe that these 3 back edges indicate 3 cycles present in
the graph.