Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 98 additions & 0 deletions graphX/BFS.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package com.graphX.graphX;

import com.graphX.graphX.helper.Pair;
import com.graphX.graphX.helper.Triple;

import java.util.*;

public class BFS {
List<Integer> minDistanceFromSource;
List<Boolean> visited;

Graph graph;
int n;

public BFS(Graph graph) {
this.graph = graph;
this.n = graph.n;
}

public void clear() {
minDistanceFromSource = new ArrayList<>(Collections.nCopies(n, -1));
visited = new ArrayList<>(Collections.nCopies(n, false));
}

public void run(int sourceR) {
int source = graph.h.hash(sourceR);
bfs(source);
}

public void run(Pair<Integer, Integer> sourceR) {
int source = graph.h.hash(sourceR);
bfs(source);
}

public void run(Triple<Integer, Integer, Integer> sourceR) {
int source = graph.h.hash(sourceR);
bfs(source);
}

public int minDistance(int targetR) {
int target = graph.h.hash(targetR);
return minDistInternal(target);
}

public int minDistance(Pair<Integer, Integer> targetR) {
int target = graph.h.hash(targetR);
return minDistInternal(target);
}

public int minDistance(Triple<Integer, Integer, Integer> targetR) {
int target = graph.h.hash(targetR);
return minDistInternal(target);
}

public boolean isVisited(int source) {
return isVisitedInternal(source);
}
public boolean isVisited(Pair<Integer, Integer> sourceR) {
int source = graph.h.hash(sourceR);
return isVisitedInternal(source);
}
public boolean isVisited(Triple<Integer, Integer, Integer> sourceR) {
int source = graph.h.hash(sourceR);
return isVisitedInternal(source);
}


private void bfs(int source) {
Queue<Integer> queue = new LinkedList<>();
queue.add(source);

minDistanceFromSource.set(source, 0);

while (!queue.isEmpty()) {
int current = queue.peek();

for (int i = 0; i < graph.adjacencyList.get(current).size(); i++) {
int adjNode = graph.adjacencyList.get(current).get(i).first;
if (!visited.get(adjNode)) {
visited.set(adjNode, true);
minDistanceFromSource.set(adjNode, minDistanceFromSource.get(current) + 1);
queue.add(adjNode);
}
}

queue.poll();
}
}

private int minDistInternal(int target) {
return minDistanceFromSource.get(target);
}

private boolean isVisitedInternal(int target) {
return visited.get(target);
}

}
95 changes: 95 additions & 0 deletions graphX/Graph.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package com.graphX.graphX;

import com.graphX.graphX.helper.Pair;
import com.graphX.graphX.helper.Triple;

import java.util.ArrayList;
import java.util.List;

public class Graph {
boolean isDirected;

public List<List<Pair<Integer, Integer>>> adjacencyList;
public int n;
public Hash h;

public Graph(int n) {
this.n = n;
this.isDirected = true;
initialize();
}

public Graph(int n, boolean isDirected) {
this.n = n;
this.isDirected = isDirected;
initialize();
}

public int hash(int u, int v) {
return h.hash(new Pair<>(u, v));
}

public int hash(int u, int v, int k) {
return h.hash(new Triple<>(u, v, k));
}

public void addEdge(int u, int v) {
addEdge(u, v, 0);
}

public void addEdge(Pair<Integer, Integer> u, Pair<Integer, Integer> v) {
addEdge(u, v, 0);
}

public void addEdge(Triple<Integer, Integer, Integer> u, Triple<Integer, Integer, Integer> v) {
addEdge(u, v, 0);
}

public void addEdge(int uR, int vR, int w) {
int u = h.hash(uR);
int v = h.hash(vR);

addEdgeInternal(u, v, w);
}

public void addEdge(Pair<Integer, Integer> uR, Pair<Integer, Integer> vR, int w) {
int u = h.hash(uR);
int v = h.hash(vR);

addEdgeInternal(u, v, w);
}

public void addEdge(Triple<Integer, Integer, Integer> uR, Triple<Integer, Integer, Integer> vR, int w) {
int u = h.hash(uR);
int v = h.hash(vR);

addEdgeInternal(u, v, w);
}

private void initialize() {
adjacencyList = new ArrayList<>();
for (int i = 0; i < n; i++) {
adjacencyList.add(new ArrayList<>());
}

h = new Hash();

}

private void addEdgeInternal(int u, int v) {
addEdgeInternal(u, v, 0);
}

private void addEdgeInternal(int u, int v, int w) {
addEdgeWeightedUndirected(u, v, w);
if (!isDirected) {
addEdgeWeightedUndirected(v, u, w);
}
}

private void addEdgeWeightedUndirected(int u, int v, int w) {
Pair<Integer, Integer> p = new Pair<>(v, w);
adjacencyList.get(u).add(p);
}

}
31 changes: 31 additions & 0 deletions graphX/Hash.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.graphX.graphX;

import com.graphX.graphX.helper.Pair;
import com.graphX.graphX.helper.Triple;

import java.util.Map;
import java.util.HashMap;

public class Hash{
private final Map<Triple, Integer> hashTable = new HashMap<>();

public Hash() {
}

public int hash(int x) {
return hash(new Triple<>(x,0,0));
}

public int hash(Pair<Integer, Integer> pair) {
return hash(new Triple<>(pair.first, pair.second, 0));
}

public int hash(Triple<Integer,Integer,Integer> triple) {
if (hashTable.containsKey(triple)) {
return hashTable.get(triple);
}
int hashVal = hashTable.size();
hashTable.put(triple, hashVal);
return hashVal;
}
}
32 changes: 32 additions & 0 deletions graphX/helper/Pair.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.graphX.graphX.helper;

import java.util.Objects;

public class Pair<A, B> {
public final A first;
public final B second;

public Pair(A first, B second) {
this.first = first;
this.second = second;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Pair<?, ?> pair)) return false;
return Objects.equals(first, pair.first) &&
Objects.equals(second, pair.second);
}

@Override
public int hashCode() {
return Objects.hash(first, second);
}

@Override
public String toString() {
return "(" + first + ", " + second + ")";
}
}

33 changes: 33 additions & 0 deletions graphX/helper/Triple.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.graphX.graphX.helper;

import java.util.Objects;

public class Triple<A, B, C> {
private final A first;
private final B second;
private final C third;

public Triple(A first, B second, C third) {
this.first = first;
this.second = second;
this.third = third;
}

@Override
public boolean equals(Object object) {
if (this == object) return true;
if (!(object instanceof Triple<?, ?, ?> triple)) return false;

return first == triple.first && second == triple.second && third == triple.third;
}

@Override
public int hashCode() {
return Objects.hash(first, second, third);
}

@Override
public String toString() {
return "(" + first + ", " + second + ", " + third + ")";
}
}