-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit f8d559d
Showing
13 changed files
with
1,004 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package Graph; | ||
|
||
import java.util.HashMap; | ||
import java.util.LinkedList; | ||
|
||
class Node{ | ||
int nodeValue; | ||
String name; | ||
Node(int nodeValue, String name){ | ||
this.nodeValue = nodeValue; | ||
this.name = name; | ||
} | ||
|
||
public Node() { | ||
|
||
} | ||
|
||
public static void addEdge(Node source, Node destination){ | ||
if(!Graph1.adjacencyMap.keySet().contains(source)){ | ||
Graph1.adjacencyMap.put(source, null); | ||
} | ||
if(!Graph1.adjacencyMap.keySet().contains(destination)){ | ||
Graph1.adjacencyMap.put(destination, null); | ||
} | ||
addEdgeHelper(source, destination); | ||
if(!Graph1.directed){ | ||
addEdgeHelper(destination, source); | ||
} | ||
} | ||
public static void addEdgeHelper(Node a, Node b){ | ||
LinkedList<Node> temp = Graph1.adjacencyMap.get(a); | ||
if(temp != null){ | ||
temp.remove(b); | ||
}else{ | ||
temp = new LinkedList<>(); | ||
} | ||
temp.add(b); | ||
Graph1.adjacencyMap.put(a, temp); | ||
} | ||
|
||
public static void printEdge(){ | ||
for(Node node : Graph1.adjacencyMap.keySet()){ | ||
System.out.print("The " + node.name + " has an edge towards: "); | ||
if(Graph1.adjacencyMap.get(node) != null){ | ||
for(Node neighbour : Graph1.adjacencyMap.get(node)){ | ||
System.out.print(neighbour.name + " "); | ||
} | ||
System.out.println(); | ||
}else{ | ||
System.out.println("none"); | ||
} | ||
} | ||
} | ||
public static boolean hasEdge(Node source, Node destination){ | ||
return Graph1.adjacencyMap.containsKey(source) && Graph1.adjacencyMap.get(source).contains(destination); | ||
} | ||
} | ||
|
||
class Graph1 extends Node{ | ||
public static HashMap<Node, LinkedList<Node>> adjacencyMap; | ||
static boolean directed; | ||
public Graph1(boolean directed){ | ||
super(); | ||
this.directed = directed; | ||
adjacencyMap = new HashMap<>(); | ||
} | ||
} | ||
public class AdjacencyListRepresentation { | ||
public static void main(String[] args) { | ||
Graph1 graph = new Graph1(true); | ||
Node a = new Node(0, "A"); | ||
Node b = new Node(1, "B"); | ||
Node c = new Node(2, "C"); | ||
Node d = new Node(3, "D"); | ||
Node e = new Node(4, "E"); | ||
|
||
Node.addEdge(a,b); | ||
Node.addEdge(b,c); | ||
Node.addEdge(b,d); | ||
Node.addEdge(c,e); | ||
Node.addEdge(b,a); | ||
|
||
Node.printEdge(); | ||
|
||
System.out.println(Node.hasEdge(a,b)); | ||
System.out.println(Node.hasEdge(d,a)); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package Graph; | ||
import java.util.ArrayList; | ||
import java.util.Scanner; | ||
|
||
/** | ||
* An adjacency list is given input as it is. ie, line number denotes vertices, | ||
* and numbers on those lines denote adjacent nodes. | ||
* Sample Input: | ||
* The first line contains a single integer n – The number of nodes. | ||
* Each of the next n lines contains first number J and followed by J space | ||
* separated integers. The ith row denote ith node j-th integer in the i-th | ||
* row denotes an adjacent to that node . | ||
* 3 | ||
* 2 1 2 | ||
* 1 2 | ||
* 2 0 1 | ||
*/ | ||
public class AdjacencyList_NormalCase { | ||
public static void main(String[] args) { | ||
Scanner sc = new Scanner(System.in); | ||
ArrayList<ArrayList<Integer>> adjList = new ArrayList<>(); | ||
System.out.println("Enter the number of nodes:"); | ||
int numberOfNodes = sc.nextInt(); | ||
|
||
for(int i=0 ; i<numberOfNodes ; i++){ | ||
adjList.add(new ArrayList<>()); | ||
int countOfConnectedNodes = sc.nextInt(); | ||
|
||
for(int j=0 ; j<countOfConnectedNodes ; j++){ | ||
int nodeValue = sc.nextInt(); | ||
adjList.get(i).add(nodeValue); | ||
} | ||
} | ||
|
||
System.out.println("Adjacency list is :"); | ||
for(int i=0 ; i<adjList.size() ; i++){ | ||
System.out.print(i + " : "); | ||
for(int k : adjList.get(i)){ | ||
System.out.print(k + " "); | ||
} | ||
System.out.println(); | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package Graph; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Scanner; | ||
|
||
/** | ||
* Graph Adjacency List Input Case 2: | ||
* An adjacency matrix is given input as it is. ie, 1 for adjacent and 0 for non-adjacent. | ||
* | ||
* Sample Input: | ||
* The first line contains a single integer n – The number of nodes. | ||
* Each of the next n lines contains n*2 space separated integers. They are pairs of adjacent nodes and weights. | ||
* 3 | ||
* 2 1 13 2 4 | ||
* 1 2 9 3 -4 | ||
* 2 0 -7 1 8 | ||
*/ | ||
public class AdjacencyList_withWeights { | ||
static class Edge{ | ||
int destinationNode, edgeWeight; | ||
|
||
// constructor | ||
public Edge(int destinationNode, int edgeWeight){ | ||
this.destinationNode = destinationNode; | ||
this.edgeWeight = edgeWeight; | ||
} | ||
} | ||
public static void main(String[] args) { | ||
Scanner sc = new Scanner(System.in); | ||
ArrayList<ArrayList<Edge>> adjList = new ArrayList<>(); | ||
System.out.println("Enter number of nodes:"); | ||
int numberOfNodes = sc.nextInt(); | ||
|
||
for(int i=0 ; i<numberOfNodes ; i++){ | ||
adjList.add(new ArrayList<>()); | ||
System.out.println("Enter count:"); | ||
int count = sc.nextInt(); | ||
for(int j=0 ; j<count ; j++) { | ||
System.out.println("Enter destination node:"); | ||
int destinationNode = sc.nextInt(); | ||
System.out.println("Enter edge weight :"); | ||
int edgeWeight = sc.nextInt(); | ||
|
||
adjList.get(i).add(new Edge(destinationNode, edgeWeight)); | ||
} | ||
} | ||
printAdjacency(adjList); | ||
} | ||
public static void printAdjacency(ArrayList<ArrayList<Edge>> adjList) { | ||
for (int i = 0; i < adjList.size(); i++) { | ||
System.out.print(i + " : "); | ||
for (Edge k : adjList.get(i)) { | ||
System.out.print(k.destinationNode + " " + k.edgeWeight); | ||
} | ||
System.out.println(); | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
package Graph; | ||
|
||
class Graph{ | ||
private int numOfNodes; | ||
private boolean directed; | ||
private boolean weighted; | ||
private float[][] matrix; | ||
|
||
/* | ||
This will allow us to safely add weighted graphs in our class since | ||
we will be able to check whether an edge exists without relying | ||
on specific special values (like 0) | ||
*/ | ||
private boolean[][] isSetMatrix; | ||
|
||
// constructor | ||
public Graph(int numOfNodes, boolean directed, boolean weighted){ | ||
this.directed = directed; | ||
this.weighted = weighted; | ||
this.numOfNodes = numOfNodes; | ||
|
||
// Simply initializes our adjacency matrix to the appropriate size | ||
matrix = new float[numOfNodes][numOfNodes]; | ||
isSetMatrix = new boolean[numOfNodes][numOfNodes]; | ||
} | ||
|
||
/* | ||
Since matrices for directed graphs are symmetrical, we have to add | ||
[destination][source] at the same time as [source][destination] | ||
Now, let's write a method that allows us to add edges. We want to make | ||
sure that in case the graph is weighted and a weight isn't provided we | ||
set the edge value to 0, and if isn't weighted to simply add 1. | ||
*/ | ||
public void addEdge(int source, int destination){ | ||
int valueToAdd = 1; | ||
if(weighted){ | ||
valueToAdd = 0; | ||
} | ||
|
||
matrix[source][destination] = valueToAdd; | ||
isSetMatrix[source][destination] = true; | ||
|
||
|
||
// if the graph is directed then | ||
if(!directed){ | ||
matrix[destination][source] = valueToAdd; | ||
isSetMatrix[destination][source] = true; | ||
} | ||
} | ||
|
||
/* | ||
In case the graph isn't weighted and a weight is provided, we simply | ||
ignore that and set the [source,destination] value to 1, indicating | ||
that an edge does exist: | ||
*/ | ||
public void addEdge(int source, int destination, float weight){ | ||
float valueToAdd = weight; | ||
if(!weighted){ | ||
valueToAdd = 1; | ||
} | ||
|
||
matrix[source][destination] = valueToAdd; | ||
isSetMatrix[source][destination] = true; | ||
|
||
// if graph is undirected then | ||
if(!directed){ | ||
matrix[destination][source] = valueToAdd; | ||
isSetMatrix[destination][source] = true; | ||
} | ||
} | ||
|
||
// method for printing adjacency matrix | ||
public void printMatrix(){ | ||
for(int i=0 ; i<numOfNodes ; i++){ | ||
for(int j=0 ; j<numOfNodes ; j++){ | ||
// We only want to print the values of those positions | ||
// that have been marked as set | ||
if(isSetMatrix[i][j]){ | ||
System.out.format("%8s", String.valueOf(matrix[i][j])); | ||
}else{ | ||
System.out.format("%8s", "/ "); | ||
} | ||
} | ||
System.out.println(); | ||
} | ||
} | ||
|
||
|
||
// a convenience method that prints out the edges in a more | ||
// understandable way | ||
public void printEdges(){ | ||
for(int i=0 ; i<numOfNodes ; i++){ | ||
System.out.print("Node " + i + " is connected to: "); | ||
for(int j=0 ; j<numOfNodes ; j++){ | ||
if(isSetMatrix[i][j]){ | ||
System.out.print(j + " "); | ||
} | ||
} | ||
System.out.println(); | ||
} | ||
} | ||
|
||
// method for checking if there is an edge or not | ||
public boolean hasEdge(int source, int destination){ | ||
return isSetMatrix[source][destination]; | ||
} | ||
|
||
// method for getting edge value | ||
public Float getEdgeValue(int source, int destination){ | ||
if(!weighted || !isSetMatrix[source][destination]){ | ||
return null; | ||
} | ||
return matrix[source][destination]; | ||
} | ||
} | ||
public class AdjacencyMatrixRepresentation { | ||
public static void main(String[] args) { | ||
Graph graph = new Graph(5, false, true); | ||
|
||
graph.addEdge(0, 2, 19); | ||
graph.addEdge(0, 3, -2); | ||
graph.addEdge(1, 2, 3); | ||
graph.addEdge(1, 3); // The default weight is 0 if weighted == true | ||
graph.addEdge(1, 4); | ||
graph.addEdge(2, 3); | ||
graph.addEdge(3, 4); | ||
|
||
// printing adjacency matrix | ||
graph.printMatrix(); | ||
|
||
System.out.println(); | ||
System.out.println(); | ||
|
||
// printing edges | ||
graph.printEdges(); | ||
|
||
System.out.println(); | ||
|
||
// checking edge is present or not | ||
System.out.println("Does an edge from 1 to 0 exist?"); | ||
if (graph.hasEdge(0,1)) { | ||
System.out.println("Yes"); | ||
}else{ | ||
System.out.println("No"); | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package Graph; | ||
import java.util.Scanner; | ||
|
||
/** | ||
* Case 1(Normal case) : | ||
* An adjacency matrix is given input as it is, i.e, 1 for adjacent nodes and 0 for non-adjacent nodes. | ||
* | ||
* Sample Input : | ||
* - The first line contains a single integer n denoting the number of nodes present in the graph. | ||
* - Each of the next n lines contains n space separated integers. The jth integer in the ith row denotes a[i][j]. | ||
* | ||
* Example : | ||
* n = 3 | ||
* 0 1 0 | ||
* 1 1 0 | ||
* 0 1 1 | ||
*/ | ||
public class AdjacencyMatrix_NormalCase { | ||
public static void main(String[] args) { | ||
Scanner sc = new Scanner(System.in); | ||
System.out.println("Enter the number of nodes:"); | ||
int numberOfNodes = sc.nextInt(); | ||
|
||
|
||
// adjacent matrix which stores true if the nodes are adjacent else stores false | ||
// dimension of adjacent matrix should be nxn where n denotes number of nodes in the graph | ||
boolean[][] adjacencyMatrixBool = new boolean[numberOfNodes][numberOfNodes]; | ||
System.out.println("Enter 1 for adjacent nodes and 0 for non-adjacent nodes:"); | ||
for(int i=0 ; i<numberOfNodes ; i++){ | ||
for(int j=0 ; j<numberOfNodes ; j++){ | ||
adjacencyMatrixBool[i][j] = (sc.nextInt() == 1); | ||
} | ||
} | ||
|
||
// printing adjacency matrix | ||
System.out.println("The adjacency matrix is:"); | ||
for(boolean[] row : adjacencyMatrixBool){ | ||
for(boolean ele : row){ | ||
System.out.print(ele + " "); | ||
} | ||
System.out.println(); | ||
} | ||
} | ||
} |
Oops, something went wrong.