Adjacency List Theory And Implementation In Java:-
In graph theory an adjacency list is a collection of unordered lists used to represent a finite graph. Each list describes the set of neighbors of a vertex in the graph. This is one of several commonly used representations of graphs for use in computer programs.
After This Article you must watch this video for proper understanding what Adjacency List is:
After watching this video you should have proper understanding of Adjacency list. And now you are able to code so please try first before you move .
Hey Have you Implemented ?
Yes : Then Comment your code in video
No : Then Watch Next video For implementing Adjacency List
Now i can Surely say that you are able to code if not then code is following :
Adjacency List for UnDirected Graph :-
public class Edge {
public int src;
public int dest;
public Edge(int src, int dest) {
this.src = src;
this.dest = dest;
}
}
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Graph1 {
public List<List<Integer>> adj = new ArrayList<List<Integer>>();
public Graph1(List<Edge> edges) {
// TODO Auto-generated constructor stub
for(int i=0;i<edges.size();i++){
adj.add(new ArrayList<Integer>());
}
for (Edge edge : edges) {
adj.get(edge.src).add(edge.dest);
// Next Line For Undirected
adj.get(edge.dest).add(edge.src);
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
List<Edge> edges = Arrays.asList(new Edge(0, 1),new Edge(1, 2),
new Edge(2, 1),new Edge(3, 2),new Edge(2, 0),
new Edge(4, 5),new Edge(5, 4));
Graph1 graph1 = new Graph1(edges);
printGraph(graph1);
}
private static void printGraph(Graph1 graph1) {
// TODO Auto-generated method stub
int src = 0;
int n = graph1.adj.size();
while(src<n){
for(int dest:graph1.adj.get(src)){
System.out.print(src+"->"+dest+"\t");
}
System.out.println();
src++;
}
}
}
Adjacency List for Directed Graph :-
public class Edge {
public int src;
public int dest;
public Edge(int src, int dest) {
this.src = src;
this.dest = dest;
}
}
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Graph1 {
public List<List<Integer>> adj = new ArrayList<List<Integer>>();
public Graph1(List<Edge> edges) {
// TODO Auto-generated constructor stub
for(int i=0;i<edges.size();i++){
adj.add(new ArrayList<Integer>());
}
for (Edge edge : edges) {
adj.get(edge.src).add(edge.dest);
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
List<Edge> edges = Arrays.asList(new Edge(0, 1),new Edge(1, 2),
new Edge(2, 1),new Edge(3, 2),new Edge(2, 0),
new Edge(4, 5),new Edge(5, 4));
Graph1 graph1 = new Graph1(edges);
printGraph(graph1);
}
private static void printGraph(Graph1 graph1) {
// TODO Auto-generated method stub
int src = 0;
int n = graph1.adj.size();
while(src<n){
for(int dest:graph1.adj.get(src)){
System.out.print(src+"->"+dest+"\t");
}
System.out.println();
src++;
}
}
}
First Watch this video for weighted Graph Implementation
Adjacency List for Weighted Graph :-
public class DEdge {
int src;
int dest;
int weight;
public DEdge(int src, int dest, int weight) {
super();
this.src = src;
this.dest = dest;
this.weight = weight;
}
}
public class Node {
int val;
int wei;
public Node(int val, int wei) {
super();
this.val = val;
this.wei = wei;
}
}
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class WeiDiGraph {
List<List<Node>> adj = new ArrayList<List<Node>>();
public WeiDiGraph(List<DEdge> edges) {
// TODO Auto-generated constructor stub
for (int i=0;i<edges.size();i++) {
adj.add(new ArrayList<Node>());
}
for (DEdge dEdge : edges) {
adj.get(dEdge.src).add(new Node(dEdge.dest, dEdge.weight));
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
List<DEdge> edges = Arrays.asList( new DEdge(0, 1, 6),
new DEdge(1, 2, 7),
new DEdge(2, 0, 5),
new DEdge(2, 1, 4),
new DEdge(3, 2, 10),
new DEdge(4, 5, 1),
new DEdge(5, 4, 3));
WeiDiGraph wdg = new WeiDiGraph(edges);
printGraph(wdg);
}
private static void printGraph(WeiDiGraph wdg) {
// TODO Auto-generated method stub
int src = 0;
int n = wdg.adj.size();
while (src<n) {
for(Node node:wdg.adj.get(src)){
System.out.print(src+"-->"+node.val+"("+node.wei+")\t");
}
System.out.println();
src++;
}
}
}
1 Comments
Thanks for the explanation! Just one doubt: Shouldn't we use
ReplyDeleteadj.get(dEdge.dest).add(new Node(dEdge.src, dEdge.weight)); also as this is undirected weighted graph?