What is a Graph?
Application of Graph |
-- Graph is a nonlinear data structure.
-- Edge and Vertex are the main components of the graph.
Type
-- Directed Graph: If an edge implies one direction then it called Directed Graph.
-- Undirected Graph: If the connection between vertices is symmetric means A connected with B ⇔ B connected with A.
Path
- we can reach India to Germany via the UK because from the above image we can't directly reach Germany.
- so (INDIA -> UK -> GERMANY) is one path of the other paths.
- Here, INDIA, UK and others are the nodes(vertices).
- This graph is directed.
One node can be connected with one node or two nodes or all nodes also in the graph.
Generate a Directed Graph (Code In Java)
class Graph {
int v; // total vertex of Graph
LinkedList<Integer> edge[];
// Constructor will assign the value of v and will
//create the v number of LinkedList
public Graph(int v) {
this.v = v;
edge = new LinkedList[v];
for (int i = 0; i < this.v; i++) {
edge[i] = new LinkedList<Integer>();
}
}
// This method is used to connecting one node to another node
// It takes three parameters
//first is graph object
//source node
//destination node
public void connectVertex(Graph g, int source, int dest) {
g.edge[source - 1].add(dest - 1);
}
//This method will print the final graph
public void showGraph(Graph g) {
for (int i = 0; i < this.v; i++) {
System.out.print(i+1);
for (int j : g.edge[i])
System.out.print("->" + (j + 1));
System.out.println();
}
}
}
int v; // total vertex of Graph
LinkedList<Integer> edge[];
// Constructor will assign the value of v and will
//create the v number of LinkedList
public Graph(int v) {
this.v = v;
edge = new LinkedList[v];
for (int i = 0; i < this.v; i++) {
edge[i] = new LinkedList<Integer>();
}
}
// This method is used to connecting one node to another node
// It takes three parameters
//first is graph object
//source node
//destination node
public void connectVertex(Graph g, int source, int dest) {
g.edge[source - 1].add(dest - 1);
}
//This method will print the final graph
public void showGraph(Graph g) {
for (int i = 0; i < this.v; i++) {
System.out.print(i+1);
for (int j : g.edge[i])
System.out.print("->" + (j + 1));
System.out.println();
}
}
}
edge is an array of LinkedList because we have to remember which node is connected with which edge.
we can define an array of LinkedList
LinkedList<E> name = new LinkedList[size_of_array]
Remain code is
public class GenerateGraph {
public static void main(String[] args) {
Graph g = new Graph(5); // This Graph have 5 nodes
g.connectVertex(g, 1, 2);
g.connectVertex(g, 2, 1);
g.connectVertex(g, 2, 3);
g.connectVertex(g, 3, 4);
g.connectVertex(g, 4, 5);
g.connectVertex(g, 5, 1);
g.showGraph(g);
}
}
public static void main(String[] args) {
Graph g = new Graph(5); // This Graph have 5 nodes
g.connectVertex(g, 1, 2);
g.connectVertex(g, 2, 1);
g.connectVertex(g, 2, 3);
g.connectVertex(g, 3, 4);
g.connectVertex(g, 4, 5);
g.connectVertex(g, 5, 1);
g.showGraph(g);
}
}
January 24, 2020
Tags :
Data Structure
Subscribe by Email
Follow Updates Articles from This Blog via Email
No Comments
Please comment here...