I've been stuck in the addEdge method of my Graph class where instead of having only one "A" Vertex in my Arraylist, it creates a new one for another edge.
For example here is my text file:
A,F,2
A,C,6
B,G,4
B,H,5
C,D,8
C,F,5
D,A,1
So instead of storing it as A->[F, C], it's A->F and A->C
Here is my Graph class
import Queue.Queue;
import Stack.Stack;
import java.util.ArrayList;
public class Graph<T>{
public ArrayList<Vertex<T>> vertexList= new ArrayList<Vertex<T>>();
public ArrayList<Edge<T>> edgeList= new ArrayList<Edge<T>>();
public void addEdge( T source, T destination, T weight){
Vertex<T>VertexA=new Vertex<T>(source);
Vertex<T> VertexB= new Vertex<T>(destination);
if (vertexList.contains(source)){
VertexA= vertexList.get(vertexList.indexOf(VertexA));
}else{
vertexList.add(VertexA);
}
if (vertexList.contains(VertexB)){
VertexB= vertexList.get(vertexList.indexOf(VertexB));
}else {
vertexList.add(VertexB);
}
VertexA.addNeighbor(VertexB);
VertexB.addNeighbor(VertexA);
edgeList.add(new Edge<T>(VertexA,VertexB,weight));
}
public void BFS(Vertex<T> startingVertex) {
startingVertex=vertexList.get(vertexList.indexOf(startingVertex));
Vertex<T> current;
Queue<Vertex<T>> myQueue= new Queue<Vertex<T>>();
ArrayList<Vertex<T>> visitedList = new ArrayList<>();
Vertex<T> startingV= vertexList.get(vertexList.indexOf(startingVertex)); // get starting vertex from vertices list\
myQueue.enqueue(startingV); // adds the starting vertex into the queue
while (!( myQueue.isEmpty())){
current = myQueue.dequeue();//removes the first element of the queue
if (!(current.isVisited())) // sets the current vertex as visited if it is not visited
current.setVisited(true);
visitedList.add(current);
// gets all the neighbors of the current vertex and adds it into the queue
for (int i=0; i<current.getNeighbors().size(); i++){
Vertex<T> neighbor = current.getNeighbors().get(i);
neighbor.getVertex();
myQueue.enqueue(neighbor);
}
}
for (int i=0; i<visitedList.size(); i++){
System.out.println(visitedList.get(i));
}
}
public void DFS(T startingVertex){
Vertex<T> startingV= vertexList.get(vertexList.indexOf(startingVertex));
Stack<Vertex<T>> myStack= new Stack<Vertex<T>>();
myStack.push(startingV);
while(! myStack.isEmpty()){
Vertex<T> current = myStack.pop();
if (!current.isVisited()){
current.setVisited(true);
System.out.println(current);
}
for(int i=0; i<current.getNeighbors().size(); i++){
myStack.push(current.getNeighbors().get(i));
}
}
}
public String toString(){
System.out.println("The Graph Contains: ");
System.out.println();
final StringBuilder builder= new StringBuilder();
for (int i=0; i<edgeList.size(); i++) {
Vertex<T> source = edgeList.get(i).getSource();
Vertex<T> destination= edgeList.get(i).getDestination();
T weight= edgeList.get(i).getWeight();
builder.append(source).append("--").append(weight).append("--").append(destination).append("\n");
}
return builder.toString();
}
public Vertex<T>getVertex(T data){
Vertex<T> vertex = new Vertex<T>(data);
return vertexList.get(vertexList.indexOf(vertex));
}
}
In the addEdge method I also tried using for loops instead of if statements, but the output is still the same.
for (int i=0; i<vertexList.size(); i++) {
if (vertexList.get(i).equals(source)) {
VertexA= vertexList.get(i);
} else {
VertexA=new Vertex<T>(source);
vertexList.add(VertexA);
}
}
for (int i=0; i<vertexList.size(); i++){
if (vertexList.get(i).equals(destination)) {
VertexB = vertexList.get(i);
} else {
VertexB=new Vertex<T>(destination);
vertexList.add(VertexB);
}
}
// I also tried using this however it results to IndexOutOfBounds
verexList.get(vertexList.indexOf(VertexA)).addNeighbor(VertexB);
verexList.get(vertexList.indexOf(VertexB)).addNeighbor(VertexB);
Here is how I read my text file in my main class using a readFile method
public static Graph<String> readFile(){
Scanner sc= new Scanner(System.in);
boolean validFile=false;
String fileName;
String fileContent="";
while (!validFile) {
try {
System.out.print("Enter the file path of the file: ");
fileName = sc.nextLine();
BufferedReader reader = new BufferedReader(new FileReader(fileName));
validFile = true;
System.out.println(fileName + " exists! ");
System.out.println();
while((fileContent=reader.readLine())!=null){
String[] row= fileContent.split(",");
graph.addEdge(row[0], row[1], row[2]);
}
} catch (FileNotFoundException exception) {
System.out.println("File does not exist");
System.out.println("Please try again. ");
} catch (IOException exception) {
exception.printStackTrace();
}
}
return graph;
}