package com.company;
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Main {
public static void main(String[] args) {
Graph<Integer> g = new Graph<>(true);
g.addEdge(0, 1, 1);
g.addEdge(0, 2, 1);
g.addEdge(0, 3, 1);
g.addEdge(4, 1, 1);
g.addEdge(4, 3, 1);
g.addEdge(4, 5, 1);
g.addEdge(4, 6,1);
g.hasEdge(0,1);
g.hasEdge(0, 4);
//g.DFS(0);
//g.DFS(4);
g.BFS(0);
}
//1 -> 2, 3
//2 -> 1
//3 -> 1
//1 -> Edge(1,2, w), Edge(1, 3, w2)
//2 -> Edge(2,1,w)
//3 -> Edge(3,1,w2)
public static class Graph<T> {
private class Edge<T>{
public T source;
public T destination;
private double weight;
public Edge() {}
public Edge(T source, T destination){
this.source = source;
this.destination = destination;
this.weight = 1;
}
public Edge(T source, T destination, double weight){
this.source = source;
this.destination = destination;
this.weight = weight;
}
public boolean isEqualTo(Edge<T> other){
if (this.source == other.source && this.destination == other.destination){
return true;
}
return false;
}
}
private HashMap<T, List<Edge<T>>> map = new HashMap<>();
private boolean undirected;
private Set<T> visited = new HashSet<>();
private HashMap<T, List<T>> path = new HashMap<>();
public void DFS(T s){
this.visited.clear();
this.path.clear();
dfs(s);
for(T node: path.keySet())
System.out.print(node + Arrays.toString(path.get(node).toArray()).replace("[","->").replace("]","") + '\n');
}
private void dfs(T vertex){
visited.add(vertex);
Iterator<Edge<T>> ite = map.get(vertex).iterator();
while (ite.hasNext()){
Edge<T> edge = ite.next();
if (!visited.contains(edge.destination)) {
if (!path.containsKey(vertex)) {
path.put(vertex, new LinkedList<T>());
}
path.get(vertex).add(edge.destination);
dfs(edge.destination);
}
}
}
public void BFS(T vertex){
HashMap<T, List<T>> path = new HashMap<>();
Set<T> visited = new HashSet<>();
LinkedList<T> queue = new LinkedList<>();
visited.add(vertex);
queue.add(vertex);
while(queue.size() != 0){
vertex = queue.poll();
Iterator<Edge<T>> ite = map.get(vertex).listIterator();
while (ite.hasNext()){
Edge<T> edge = ite.next();
if (!visited.contains(edge.destination)){
visited.add(edge.destination);
queue.add(edge.destination);
if (!path.containsKey(vertex)) {
path.put(vertex, new LinkedList<>());
}
path.get(vertex).add(edge.destination);
}
}
}
for(T node: path.keySet())
System.out.print(node + Arrays.toString(path.get(node).toArray()).replace("[","->").replace("]","") + '\n');
}
public Graph(boolean undirected) {
this.undirected = undirected;
}
public void addVertex(T s){
map.put(s, new LinkedList<Edge<T>>());
}
public void addEdge(T source, T destination, double weight){
if (!map.containsKey(source))
addVertex(source);
if (!map.containsKey(destination))
addVertex(destination);
map.get(source).add(new Edge<T>(source, destination, weight));
if (this.undirected == true){
map.get(destination).add(new Edge<T>(destination, source, weight));
}
}
public void getVertexCount() {
System.out.println("The number of nodes is " + map.keySet().size());
}
public void getEdgeCount() {
int count = 0;
for (T v : map.keySet()) {
count += map.get(v).size();
}
if (this.undirected == true) {
count = count / 2;
}
System.out.println("the number of edges is " + count);
}
public void hasVertex(T s) {
if (map.containsKey(s)) {
System.out.println("The nodes is in the graph");
} else {
System.out.println("The nodes is not in the graph");
}
}
//1 -> Edge(1,2, w), Edge(1, 3, w2), Edge(1, 4, w3)
//2 -> Edge(2,1,w)
//3 -> Edge(3,1,w2)
//4 -> Edge(4, 1, w3)
//temp_edge = Edge(1, 3)
public void hasEdge(T source, T destination){
Edge<T> temp_edge = new Edge<T>(source, destination);
List<Edge<T>> list = map.get(source);
for (Edge<T> e: list){
if (e.isEqualTo(temp_edge)){
System.out.println("The edge is part of the graph");
return;
}
}
System.out.println("The edge is not a part of the graph");
}
}
}