Created
February 21, 2017 20:48
-
-
Save pbesra/0613f41ca05b972a16b74e15c421b3c5 to your computer and use it in GitHub Desktop.
adjacency matrix implementation using 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
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(3); | |
g.insertEdge(0, 1, 10); | |
g.insertEdge(0, 2, 12); | |
g.insertEdge(1, 2, 9); | |
g.printData(); | |
} | |
} | |
class Graph{ | |
public int mat[][]; | |
public int m=0; | |
public Graph(int n){ | |
this.mat=new int[n][n]; | |
for(int i=0;i<n;i++){ | |
for(int j=0;j<n;j++){ | |
mat[i][j]=0; | |
} | |
} | |
this.m=n; | |
} | |
public void insertEdge(int u, int v, int w){ | |
mat[u][v]=w; | |
mat[v][u]=w; | |
} | |
public void printData(){ | |
for(int i=0;i<m;i++){ | |
for(int j=0;j<m;j++){ | |
System.out.print(mat[i][j]+" "); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment