Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import com.sun.xml.internal.bind.v2.runtime.unmarshaller.XsiNilLoader;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.PrintWriter;
- import java.util.Arrays;
- import java.util.Scanner;
- /*
- Team # 1
- Task | Name | Id
- part 1 | Aiman Alghamdi | 1535685
- part 2 | Mubarak Nawar | 1316585
- part 3 | Yousef Bajafar |1324308
- */
- /**
- *
- * @author Asus
- */
- public class AddGraph //Main Class
- {
- /**
- *
- * @param args
- * @throws FileNotFoundException
- */
- public static void main(String[] args) throws FileNotFoundException {
- //Create an object ‘theGraph’ of Graph class
- //Write commands for getting the vertices and edges to be added in the graph ,
- //from the user here. And call the appropriate methods to add the vertices and
- //edges in the graph here
- File file = new File("input.in");
- Scanner input = new Scanner(file);
- PrintWriter output = new PrintWriter("output.out");
- // Enter weight or not
- String quit=input.next();
- int numberOfVerticies =0;
- while(!quit.equalsIgnoreCase("quit")){
- String weight=quit;
- boolean checkWeight=false;
- if(weight.equalsIgnoreCase("WEIGHTED")){
- checkWeight=true;
- }
- //write a loop and ask the user how many verticies you want to add
- // number of verticies
- numberOfVerticies = input.nextInt();
- Graph theGraph = new Graph(numberOfVerticies);
- int counter = 0;
- for (int i = 0; i < numberOfVerticies; i++) {
- String vertix = i+"";
- theGraph.addVertex(Integer.parseInt(vertix.charAt(0)+""));
- counter++;
- }
- int number = input.nextInt();
- for (int i = 0; i < number; i++) {
- int one = input.nextInt();
- int two = input.nextInt();
- if(checkWeight){
- int weightNum=input.nextInt();
- theGraph.addEdgeWeight(one, two, weightNum);
- }else{
- theGraph.addEdgeUnWeight(one, two);
- }
- }
- quit=input.next();
- //add the verticies within the look, the loop length is a counter
- if(checkWeight){
- output.println("Weight Matrix: ");
- theGraph.displayGraph(output);
- output.println();
- }else{
- output.println("Adjacency Matrix: ");
- theGraph.displayGraph(output);
- output.println();
- }
- theGraph.DisplayADJACENCYMat(output);
- output.print("\n# of vertices is: "+numberOfVerticies+", # of edges is:"+number+"\n");
- theGraph.DisplayADJACENCYList(output);
- //Call the method displayGraph to display the graph as adjacency matric
- output.print("DFS Traversal: ");
- theGraph.dfs(output);
- theGraph.unvisted();
- output.println();
- output.print("\nBFS Traversal: ");
- theGraph.bfs(output);
- output.println();
- theGraph.dijkstraWithHeap(numberOfVerticies,output);
- output.println("\n-----------------------------------------");
- theGraph.dijkstraWithUnordered(numberOfVerticies,output);
- if (theGraph.connected()) {
- output.println("\nGraph is connected");
- } else {
- output.println("\nGraph is not connected");
- }
- }
- //
- input.close();
- output.close();
- } // end main()
- } // end class AddGraph
- class Graph //Class to implement graph
- {
- private final int MAX_VERTS = 20;
- private Vertex vertexList[]; // Array of vertices as objects of class Vertex
- private int adjMat[][]; // adjacency matrix
- private int nVerts; // current number of vertices
- public static boolean connected = true;
- // -----------------------------------------------------------
- public Graph(int x) // constructor
- {
- vertexList = new Vertex[x];
- adjMat = new int[x][x];
- nVerts = 0;
- //Write statements here to initialize the above data members
- } // end constructor
- // -----------------------------------------------------------
- public void addVertex(int lab) {
- //Write statement(s) here to add new vertex
- vertexList[nVerts++] = new Vertex(lab);
- }
- public String getVertex(int x) {
- return vertexList[x].label + "";
- }
- // -----------------------------------------------------------
- public void addEdgeUnWeight(int start, int end) {
- //Write statement(s) here to add new edge for undirected graph
- adjMat[start][end] = 1;
- // adjMat[end][start] = 1;
- }
- // ------------------------------------------------------------
- public void addEdgeWeight(int start, int end,int weight) {
- //Write statement(s) here to add new edge for undirected graph
- adjMat[start][end] = weight;
- // adjMat[end][start] = weight;
- }
- // ------------------------------------------------------------
- public void displayVertex(int v,PrintWriter output) {
- //Write statement(s) here to display the vertex label
- output.print(" " + vertexList[v].label);
- }
- // ------------------------------------------------------------
- public void displayGraph(PrintWriter output) {
- displayGraph(adjMat,output);
- }
- private void displayGraph(int[][] adjMat,PrintWriter output) {
- //Write statements here to display the adjacency matrix of the given graph like the output given below
- output.print(" ");
- for (int i = 0; i < nVerts; i++) {
- output.print(" ");
- displayVertex(i,output);
- output.print(" |");
- }
- output.println("");
- for (int i = 0; i < nVerts; i++) {
- displayVertex(i,output);
- output.print("|");
- for (int j = 0; j < nVerts; j++) {
- output.printf(" %2d |" , adjMat[i][j] );
- }
- output.println("");
- }
- }
- public void DisplayADJACENCYMat(PrintWriter output) {
- DisplayADJACENCYMat(adjMat, vertexList,output);
- }
- private void DisplayADJACENCYMat(int[][] adjMat, Vertex vertexList[], PrintWriter output) {
- for (int i = 0; i < nVerts; i++) {
- //VERTEX: 0 {a} - VISIT: false - ADJACENCY: 3,2,4
- output.print("VERTEX: " + i + " {" + getVertex(i) + "} -VISIT: " + vertexList[i].wasVisited + " -ADJACENCY: ->>");
- for (int j = 0; j < nVerts; j++) {
- if (adjMat[i][j] != 0) {
- displayVertex(j,output);
- }
- }
- output.println("");
- }
- }
- public void DisplayADJACENCYList(PrintWriter output) {
- DisplayADJACENCYList(adjMat, vertexList,output);
- }
- private void DisplayADJACENCYList(int[][] adjMat, Vertex vertexList[], PrintWriter output) {
- for (int i = 0; i < nVerts; i++) {
- output.print(i+":");
- for (int j = 0; j < nVerts; j++) {
- if (adjMat[i][j] != 0) {
- output.print(" "+ getVertex(i) + "-" +getVertex(j) +" "+(double) adjMat[i][j]);
- }
- }
- output.println("");
- }
- output.println("");
- }
- public boolean connected() {
- return connected;
- }
- void bfs(PrintWriter output) // breadth-first search
- {
- QueueArray queue = new QueueArray(nVerts);
- queue.enqueue(0);
- while (!queue.isEmpty()) {
- int v1 = queue.dequeue();
- vertexList[v1].wasVisited = true;
- for (int j = 0; j < nVerts; j++) {
- if ((adjMat[v1][j] != 0) && !vertexList[j].wasVisited) {
- queue.enqueue(j);
- vertexList[j].wasVisited = true;
- }
- }
- displayVertex(v1,output);
- if (queue.isEmpty()) {
- for (int i = 0; i < nVerts; i++) {
- if (vertexList[i].wasVisited == false) {
- connected = false;
- queue.enqueue(i);
- vertexList[i].wasVisited = true;
- break;
- }
- }
- }
- }
- } // end bfs()
- //===========================================
- void dfs(PrintWriter output) {
- StackLL stack = new StackLL();
- stack.push(0);
- // String temp = "0 ";
- // stack.pop();
- int count = 0;
- displayVertex(stack.peek(),output);
- while (!stack.isEmpty()) {
- int v1 = stack.peek();
- vertexList[v1].wasVisited = true;
- for (int j = 0; j < nVerts; j++) {
- if ((adjMat[v1][j] != 0) && !vertexList[j].wasVisited) {
- stack.push(j);
- vertexList[j].wasVisited = true;
- v1 = j;
- displayVertex(v1,output);
- j = 0;
- count++;
- }
- }
- // displayVertex(v1);
- stack.pop();
- if (stack.isEmpty()) {
- for (int i = 0; i < nVerts; i++) {
- if (vertexList[i].wasVisited == false) {
- stack.push(i);
- displayVertex(stack.peek(),output);
- vertexList[i].wasVisited = true;
- count++;
- break;
- }
- }
- }
- }
- //System.out.println("count "+ count);
- }//End of DFS method
- void unvisted() {
- for (int i = 0; i < nVerts; i++) {
- vertexList[i].wasVisited = false;
- }
- }
- public void dijkstraWithHeap(int x,PrintWriter output){
- minHeap q = new minHeap(x);
- // for (int i = 0; i < x; i++) {
- // vertexList[i].setDis(Integer.MAX_VALUE);
- // vertexList[i].src=null;
- // q.insert(vertexList[i], Integer.MAX_VALUE);
- // }
- for (int i = 0; i < x; i++) {
- vertexList[i].wasVisited=false;
- }
- q.insert(vertexList[0], 0);
- vertexList[0].wasVisited=true;
- vertexList[0].src="0 ";
- Vertex[]array=new Vertex[x];
- int counter1=0;
- int counter =0;
- while(!q.isEmpty()) {
- for (int i = 0; i < x; i++) {
- if(adjMat[counter][i]>0&&!vertexList[i].wasVisited){
- q.insert(vertexList[i], Integer.MAX_VALUE);
- vertexList[i].wasVisited=true;
- vertexList[i].src=vertexList[counter].src+vertexList[i].label+" ";
- double z=adjMat[counter][i]+vertexList[counter].getDis();
- if(z<vertexList[i].getDis()){
- q.decreaseKey(vertexList[i],(int)z);
- }
- }
- }
- array[counter1++]=q.deleteMin();
- for (int i = 0; i < x; i++) {
- if(!q.isEmpty()&&vertexList[i].label==q.peek().label){
- counter=i;
- break;
- }
- }
- //
- // Vertex ux = q.peek();
- // array[counter++]=ux;
- //
- // for (int j = 0; j < x; j++) {
- // if(adjMat[ux.label][j]>0){
- // if(ux.getDis()+adjMat[ux.label][j]<vertexList[j].getDis()){
- // vertexList[j].src=ux;
- // // System.out.println(vertexList[j].label+" "+vertexList[j].src.label+" new0 "+vertexList[j].getDis());
- // q.decreaseKey(vertexList[j],ux.getDis()+adjMat[ux.label][j]);
- // System.out.print("from "+ux.label+" to "+vertexList[j].label +" src ");
- // if(ux.src==null){
- // System.out.println("null");
- // }else
- // System.out.println(ux.src.label);
- //
- //
- // }
- // }
- //
- // }
- // q.remove();
- }
- Arrays.sort(array);
- output.println("\nShortest paths from 0 are:");
- for (int i = 0; i < x; i++) {
- //Shortest paths from 0 are:
- //A path from 0 to 0: 0 (Length: 0.0)
- output.println("A path from 0 to "+array[i].label+": "+array[i].src+" (Length: "+array[i].getDis()+")");
- }
- }
- public void dijkstraWithUnordered(int x, PrintWriter output){
- Unoedered q = new Unoedered(x);
- // for (int i = 0; i < x; i++) {
- // vertexList[i].setDis(Integer.MAX_VALUE);
- // vertexList[i].src=null;
- // q.insert(vertexList[i], Integer.MAX_VALUE);
- // }
- for (int i = 0; i < x; i++) {
- vertexList[i].wasVisited=false;
- }
- q.insert(vertexList[0], 0);
- vertexList[0].wasVisited=true;
- vertexList[0].src="0 ";
- Vertex[]array=new Vertex[x];
- int counter1=0;
- int counter =0;
- while(!q.isEmpty()) {
- for (int i = 0; i < x; i++) {
- if(adjMat[counter][i]>0&&!vertexList[i].wasVisited){
- q.insert(vertexList[i], Integer.MAX_VALUE);
- vertexList[i].wasVisited=true;
- vertexList[i].src=vertexList[counter].src+vertexList[i].label+" ";
- double z=adjMat[counter][i]+vertexList[counter].getDis();
- if(z<vertexList[i].getDis()){
- q.decreaseKey(vertexList[i],(int)z);
- }
- }
- }
- array[counter1++]=q.deleteMin();
- for (int i = 0; i < x; i++) {
- if(!q.isEmpty()&&vertexList[i].label==q.peek().label){
- counter=i;
- break;
- }
- }
- //
- // Vertex ux = q.peek();
- // array[counter++]=ux;
- //
- // for (int j = 0; j < x; j++) {
- // if(adjMat[ux.label][j]>0){
- // if(ux.getDis()+adjMat[ux.label][j]<vertexList[j].getDis()){
- // vertexList[j].src=ux;
- // // System.out.println(vertexList[j].label+" "+vertexList[j].src.label+" new0 "+vertexList[j].getDis());
- // q.decreaseKey(vertexList[j],ux.getDis()+adjMat[ux.label][j]);
- // System.out.print("from "+ux.label+" to "+vertexList[j].label +" src ");
- // if(ux.src==null){
- // System.out.println("null");
- // }else
- // System.out.println(ux.src.label);
- //
- //
- // }
- // }
- //
- // }
- // q.remove();
- }
- Arrays.sort(array);
- output.println("\nShortest paths from 0 are:");
- for (int i = 0; i < x; i++) {
- //Shortest paths from 0 are:
- //A path from 0 to 0: 0 (Length: 0.0)
- output.println("A path from 0 to "+array[i].label+": "+array[i].src+" (Length: "+array[i].getDis()+")");
- }
- }
- }// End of Graph class
- // Class from which we can create Stack node objects
- class StackNode {
- private int data;
- private StackNode next;
- // CONSTRUCTORS
- public StackNode() {
- data = 0;
- next = null;
- }
- public StackNode(int data) {
- this.data = data;
- next = null;
- }
- public StackNode(int data, StackNode next) {
- this.data = data;
- this.next = next;
- }
- // ACCESSORS
- public int getData() {
- return data;
- }
- public StackNode getNext() {
- return next;
- }
- // MUTATORS
- public void setData(int data) {
- this.data = data;
- }
- public void setNext(StackNode next) {
- this.next = next;
- }
- } //End class Stack
- //Class from which we can create fully functional Stacks (using linked lists)
- class StackLL {
- // top: reference variable to the top of the stack (same as "head" of linked list)
- private StackNode top;
- // CONSTRUCTOR
- public StackLL() {
- top = null;
- }
- /* Below are MANY methods that are used on stacks.
- *
- * Examples:
- * isEmpty, PUSH, POP, PEEK, and more.
- */
- //
- // boolean | isEmpty()
- //
- public boolean isEmpty() {
- return top == null;
- }
- //
- // void | PrintStack()
- //
- public void PrintStack(PrintWriter output) {
- PrintStack(top,output);
- }
- //
- // void | PrintStack(StackNode)
- //
- private void PrintStack(StackNode top,PrintWriter output) {
- // We need to traverse...so we need a help ptr
- StackNode helpPtr = top;
- // Traverse to correct insertion point
- while (helpPtr != null) {
- // Print the data value of the node
- output.print(helpPtr.getData() + ", ");
- // Step one node over
- helpPtr = helpPtr.getNext();
- }
- System.out.println();
- }
- //
- // boolean | search(int)
- //
- public boolean search(int data) {
- return search(top, data);
- }
- //
- // boolean | search(StackNode, int)
- //
- private boolean search(StackNode p, int data) {
- // To search, we must traverse. Therefore, we need helpPtr.
- StackNode helpPtr = p;
- while (helpPtr != null) {
- if (helpPtr.getData() == data) {
- return true;
- }
- helpPtr = helpPtr.getNext(); // step one node over
- }
- return false;
- }
- //
- // void | push(int)
- //
- public void push(int data) {
- top = push(top, data);
- }
- //
- // StackNode | push(StackNode, int)
- //
- private StackNode push(StackNode top, int data) {
- // Make a new StackNode with "data" as the data value
- // and set the "next" of this new node to the same address as top
- // * This is the same as addToFront() method for Linked Lists.
- top = new StackNode(data, top);
- // Now, return the newly updated top.
- return top;
- }
- //
- // StackNode | pop()
- //
- public StackNode pop() {
- // Save a reference to the current top node (because we will change where top points to)
- StackNode temp = top;
- // Now, invoke the pop method with top as a parameter.
- // This method will return a new top node.
- top = pop(top);
- // Finally, return temp, which is the previous top node that we just "popped" off the list.
- return temp;
- }
- //
- // StackNode | pop(StackNode)
- //
- private StackNode pop(StackNode top) {
- // Set top equal to the next node.
- // This will make top point to the 2nd node instead of the first node.
- top = top.getNext();
- // return the address/reference of the new top node
- return top;
- }
- //
- // int | peek()
- //
- public int peek() {
- // Invoke the peek method with top as a parameter
- int topValue = peek(top);
- // return topValue
- return topValue;
- }
- //
- // int | peek(StackNode)
- //
- private int peek(StackNode top) {
- // Return the data value of the top node.
- // You can see that we do NOT pop. We are only returning the data value.
- return top.getData();
- }
- } // End class StackLL
- //=================================================
- //Class from which we can create fully functional Queues (using arrays)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement