Created
February 23, 2017 12:58
-
-
Save pbesra/a679c63bdb20df9be62048fabcc83ff1 to your computer and use it in GitHub Desktop.
custom adjacency list representation in java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//directed graph with edge weight between vertex | |
import java.util.*; | |
import java.util.regex.Matcher; | |
import java.util.regex.Pattern; | |
import java.lang.*; | |
import java.awt.List; | |
import java.io.*; | |
import java.time.*; | |
import java.math.*; | |
import java.rmi.Remote; | |
import java.sql.Array; | |
public class MyClass { | |
public static void main(String[] args)throws java.lang.Exception{ | |
Graph g=new Graph(4); | |
g.insertEdge(0, 1, 18); | |
g.insertEdge(0, 2, 17); | |
g.insertEdge(1, 2, 15); | |
g.printGraph(); | |
} | |
} | |
class Graph{ | |
public int m; | |
public node nodeList[]; | |
public Graph(int n){ | |
this.m=n; | |
nodeList=new node[this.m]; | |
for(int i=0;i<this.m;i++){ | |
nodeList[i]=null; | |
} | |
} | |
public void insertEdge(int u, int v, int w){ | |
node nd=new node(v, w); | |
nd.next=nodeList[u]; | |
nodeList[u]=nd; | |
} | |
public void printGraph(){ | |
for(int i=0;i<m;i++){ | |
node t=nodeList[i]; | |
while(t!=null){ | |
System.out.println(i+" "+t.dest+" "+t.weight); | |
t=t.next; | |
} | |
} | |
} | |
} | |
class node{ | |
public node next; | |
//public node head=null; | |
public int dest; | |
public int weight=0; | |
public node(int u, int w){ | |
this.dest=u; | |
this.weight=w; | |
this.next=null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment