diff --git a/friends/Graph_Methods.java b/friends/Graph_Methods.java index 46dc4b2..439c94c 100644 --- a/friends/Graph_Methods.java +++ b/friends/Graph_Methods.java @@ -1,199 +1,12 @@ -package graph; - -import java.io.*; -import java.util.StringTokenizer; - -/** - * This class implements a term of a polynomial. - * - * @author runb-cs112 - * - */ -class Term { - /** - * name of term. - */ - public String name; - - /** - * school of term. - */ - public String school; - - /** - * Initializes an instance with given name and school. - * - * @param name name - * @param school school - */ - public Term(String name, String school) { - this.name = name; - this.school = school; - } - - /* (non-Javadoc) - * @see java.lang.Object#equals(java.lang.Object) - */ - public boolean equals(Object other) { - return other != null && - other instanceof Term && - name == ((Term)other).name && - school == ((Term)other).school; - } - - /* (non-Javadoc) - * @see java.lang.Object#toString() - */ - public String toString() { - if (school == "") { - return name + ""; - } else if (school == 1) { - return name + "x"; - } else { - return name + "x^" + school; - } - } -} - -/** - * This class implements a linked list node that contains a Term instance. - * - * @author runb-cs112 - * - */ -class Node { - - /** - * Term instance. - */ - Term term; - - /** - * Next node in linked list. - */ - Node next; - - /** - * Initializes this node with a term with given name and school, - * pointing to the given next node. - * - * @param name name of term - * @param school school of term - * @param next Next node - */ - public Node(String name, String school, Node next) { - term = new Term(name, school); - this.next = next; - } -} - -/** - * This class implements a polynomial. - * - * @author runb-cs112 - * - */ -public class Graph_Methods { - - /** - * Pointer to the front of the linked list that stores the polynomial. - */ - Node poly; - - /** - * Initializes this polynomial to empty, i.e. there are no terms. - * - */ - public Graph_Methods() { - poly = null; - } - - /** - * Reads a polynomial from an input stream (file or keyboard). The storage format - * of the polynomial is: - *
-     *      
-     *      
-     *     ...
-     *      
-     * 
- * with the guarantee that schools will be in descending order. For example: - *
-     *      4 5
-     *     -2 3
-     *      2 1
-     *      3 0
-     * 
- * which represents the polynomial: - *
-     *      4*x^5 - 2*x^3 + 2*x + 3
-     * 
- * - * @param br BufferedReader from which a polynomial is to be read - * @throws IOException If there is any input error in reading the polynomial - */ - public Polynomial(BufferedReader br) throws IOException { - String line; - StringTokenizer tokenizer; - float name; - int school; - - poly = null; - - while ((line = br.readLine()) != null) { - tokenizer = new StringTokenizer(line); - name = Float.parseFloat(tokenizer.nextToken()); - school = Integer.parseInt(tokenizer.nextToken()); - poly = new Node(name, school, poly); - } - } - - public build() - - public Person subgraph(String school) { - /** COMPLETE THIS METHOD **/ - return null; - } - - /** - * Returns the polynomial obtained by multiplying the given polynomial p - * with this polynomial - DOES NOT change this polynomial - * - * @param p Polynomial with which this polynomial is to be multiplied - * @return A new polynomial which is the product of this polynomial and p. - */ - public Polynomial multiply(Polynomial p) { - /** COMPLETE THIS METHOD **/ - return null; - } - - /** - * Evaluates this polynomial at the given value of x - * - * @param x Value at which this polynomial is to be evaluated - * @return Value of this polynomial at x - */ - public float evaluate(float x) { - /** COMPLETE THIS METHOD **/ - return 0; - } - - /* (non-Javadoc) - * @see java.lang.Object#toString() - */ - public String toString() { - String retval; - - if (poly == null) { - return "0"; - } else { - retval = poly.term.toString(); - for (Node current = poly.next ; - current != null ; - current = current.next) { - retval = current.term.toString() + " + " + retval; - } - return retval; - } - } +package friends; + +public class Friendex { + public int friendNum; + public Friendex next; + + public Friendex(int friendNum, Friendex next){ + this.friendNum = friendNum; + this.next = next; + } + } diff --git a/friends/Graph_Test.java b/friends/Graph_Test.java index 7004440..2c5f617 100644 --- a/friends/Graph_Test.java +++ b/friends/Graph_Test.java @@ -1,97 +1,386 @@ +/*Written by Stewart Smith and Jake Skelci for CS 112 Fall 2012 + * + * Uses BFS and DFS to find subgraphs, shortest paths, cliques, and connectors + * */ package friends; import java.io.*; -import java.util.ArrayList; -import java.util.Scanner; - - -public class Graph_Test { - static BufferedReader br1, br2; - static Scanner stdin = new Scanner(System.in); - - public static final int STUDENTS = 1; - public static final int SHORT = 2; - public static final int CLIQUE = 3; - public static final int CONNECT = 4; - public static final int QUIT = 5; - - public static int getChoice() - throws IOException { - System.out.println(); - System.out.println(STUDENTS + ". Students at school"); - System.out.println(SHORT + ". Shortest intro chain"); - System.out.println(CLIQUE + ". Cliques at school"); - System.out.println(CONNECT + ". Connectors"); - System.out.println(QUIT + ". QUIT"); - System.out.print("\tEnter choice # => "); - return (Integer.parseInt(br1.readLine())); - } - - public static void subgraph() - throws IOException { - System.out.print("Enter the name of the school => "); - String sc = stdin.nextLine(); - - } - - public static void shortest() - throws IOException { - System.out.print("Enter the name of the lonely boy => "); - String lone = stdin.nextLine(); - - } - - public static void cliques() - throws IOException { - System.out.println("Enter the name of the school => "); - String x = stdin.nextLine(); - - } - - public static void connectors(){ - - - } - - public static void main(String[] args) throws IOException { - br1 = new BufferedReader(new InputStreamReader(System.in)); - System.out.print("Enter the name of the friendship file => "); - br2 = new BufferedReader(new FileReader(br1.readLine())); - - int count = Integer.parseInt(br2.readLine())+1; //number of people - String[] personBuk = new String[count]; - - for(int i=0; i friendsBuk = new ArrayList(); - String line = br2.readLine(); - friendsBuk.add(line); - - while(line != null){ //put the friends line in an array - line = br2.readLine(); - friendsBuk.add(line); - } - - //build graph here - - int choice = getChoice(); - while (choice != QUIT) { - if (choice < 1 || choice > QUIT) { - System.out.println("\tIncorrect choice " + choice); - } else { - switch (choice) { - case STUDENTS: subgraph(); break; - case SHORT: shortest(); break; - case CLIQUE: cliques(); break; - case CONNECT: connectors(); break; //revise - default: break; - } - } - choice = getChoice(); - } - - } -} \ No newline at end of file +import java.util.*; +import java.util.Queue; + +public class Friends { + static BufferedReader br1, br2; + static Scanner stdin = new Scanner(System.in); + + public static final int STUDENTS = 1; + public static final int SHORT = 2; + public static final int CLIQUE = 3; + public static final int CONNECT = 4; + public static final int QUIT = 5; + + public static int getChoice() + throws IOException { + System.out.println(); + System.out.println(STUDENTS + ". Students at school"); + System.out.println(SHORT + ". Shortest intro chain"); + System.out.println(CLIQUE + ". Cliques at school"); + System.out.println(CONNECT + ". Connectors"); + System.out.println(QUIT + ". QUIT"); + System.out.print("\tEnter choice # => "); + return (Integer.parseInt(br1.readLine())); + } + + public static void main(String[] args) throws IOException { + br1 = new BufferedReader(new InputStreamReader(System.in)); + System.out.print("Enter the name of the friendship file => "); + br2 = new BufferedReader(new FileReader(br1.readLine())); + + int count = Integer.parseInt(br2.readLine())+1; //number of people + String[] personBuk = new String[count-1]; + + for(int i=0; i friendsBuk = new ArrayList(); + String line = br2.readLine(); + friendsBuk.add(line); + + while(line != null){ //put the friends line in an array + line = br2.readLine(); + friendsBuk.add(line); + } + + Hashtable hash = new Hashtable(); + Person[] zoo = new Person[count]; + zoo = build(personBuk, friendsBuk, hash); + + int choice = getChoice(); + while (choice != QUIT) { + if (choice < 1 || choice > QUIT) { + System.out.println("\tIncorrect choice " + choice); + } else { + switch (choice) { + case STUDENTS: + System.out.print("Enter the name of the school => "); + String school = stdin.nextLine(); + subgraph(school, zoo, true); + break; + case SHORT: + System.out.print("Enter the name of the starting person => "); + String start = stdin.nextLine(); + System.out.print("Enter the name of the target person => "); + String target = stdin.nextLine(); + shortest(start, target, zoo, hash); + break; + case CLIQUE: + System.out.print("Enter the name of the school => "); + String sc = stdin.nextLine(); + cliques(sc, zoo); + break; + case CONNECT: + connectors(zoo, count); + break; + default: break; + } + } + choice = getChoice(); + for(int i=0; i friends, Hashtable hash){ + Person[] zoo = new Person[people.length]; + for(int i=0; i subgraph(String school, Person[] zoo, boolean printSub){ + + ArrayList schoolZoo = new ArrayList<>(); + int schoolZoodex = 0; + for(int i=0; i< zoo.length; i++){ //go through all people in zoo----linear + if(zoo[i].school != null && zoo[i].school.equalsIgnoreCase(school)){ //if their school matches + zoo[i].schoolIndex=schoolZoodex; + + Person tempPer = new Person(zoo[i].name, zoo[i].school, zoo[i].front.copyList(), false, i,schoolZoodex,-1,-1); //copy zoo person + schoolZoo.add(tempPer); //add the person to the arrayList + schoolZoodex++; + } + } + for(int k=0; k schoolZoo){ + //boolean[] visited = new boolean[(schoolZoo.size())]; + for(int i=0; i hash)throws IOException { + Person perStart = null; + Stack printStack = new Stack(); + Queue newQ = new LinkedList(); + boolean complete = false; //SS + + int i = hash.get(start.toLowerCase()); // finds the start person in zoo + perStart = zoo[i]; + perStart.zooIndex = i; + newQ.add(i); // add the start person to queue for BFS + + while (!newQ.isEmpty()){ //BFS queue build + if(complete) break; //SS + zoo[newQ.peek()].visited = true; + Person parent = zoo[newQ.poll()]; //dequeue front + Friendex friendPtr = parent.front; + while (friendPtr!=null){ //move horizontally through friends + Person temp = zoo[friendPtr.friendNum]; //friend currently looking at + if (!temp.visited){ //unvisited friends + temp.zooIndex = friendPtr.friendNum; + temp.back = parent.zooIndex; //tells where the person came from + } + else{ //if friend has already been visited + friendPtr = friendPtr.next; + continue; + } + if (target.equalsIgnoreCase(temp.name)){ // target is found + Person parentPtr = temp; + while (parentPtr.zooIndex != perStart.zooIndex){ //moves back and populates print stack + printStack.push(parentPtr); + parentPtr=zoo[parentPtr.back]; + } + printStack.push(perStart); // SS----adds last starting person to printStack + complete = true; //SS + break; // SS--- target found so break out of loop + }else {newQ.add(friendPtr.friendNum); //if temp is not the target put on BFS queue + friendPtr=friendPtr.next; + } + } + } + String answer = ""; + if(printStack.isEmpty()){ + System.out.println("No possible path"); + } + else{ + while (!printStack.isEmpty()){ + String name = printStack.pop().name; + answer += name + "--"; + } + answer = answer.substring(0,answer.length()-2); + System.out.println(answer); + } + } //end shortest + + + public static void cliques(String school, Person[] zoo){ + + ArrayList schoolZoo = subgraph(school, zoo, false); //creates subgraph with school + ArrayList> answer = new ArrayList<>(); + + for(int i=0; i< schoolZoo.size(); i++){ //go through vertical array + Person vert = schoolZoo.get(i); + Queue newQ = new LinkedList(); + ArrayList newClique = new ArrayList<>(); + boolean addClique = false; + if(!vert.visited){ //the person has not been visited so he must be part of a new clique + addClique =true; + newQ.add(vert); + while(!newQ.isEmpty()){ //BFS queue + Person justDQd = newQ.remove(); + justDQd.visited = true; + newClique.add(justDQd); + Friendex ptr = justDQd.front; + while(ptr != null){ //go through LL horizontally + if(!schoolZoo.get(ptr.friendNum).visited){ + newQ.add(schoolZoo.get(ptr.friendNum)); + } + ptr = ptr.next; + } + } + } + if(addClique){ + answer.add(newClique); + } + } + //Print out the cliques and members + for(int p=0; p clique = answer.get(p); + System.out.println("Clique " + (p+1) +":"); + for(int k=0; k conn = new ArrayList(); + boolean[] conn = new boolean[zoo.length]; + + for(int i=0; i dfsStack = new Stack(); + dfsStack.push(dfsStart); + while(!dfsStack.isEmpty()){ //stack allows forward movement when push and backward when popped + Person curr = dfsStack.peek(); + if(!curr.visited){ + curr.dfs = count; + curr.back = count; + count++; + curr.visited = true; + } + Friendex ptr = curr.front; + while(ptr != null){ //moves horizontally through friends + if(!zoo[ptr.friendNum].visited){ //go to the friend that isn't already visited + Person currFriend = zoo[ptr.friendNum]; + dfsStack.push(currFriend); //moving forward in graph + break; + }else{ //neighbor is already visited + Person base = curr; + Person neigh = zoo[ptr.friendNum]; + base.back = Math.min(base.back, neigh.dfs); + } + ptr = ptr.next; + } + if(ptr == null){ //end of the dfs line---- start going backwards + if(curr.dfs == dfsStart.dfs){ + break; //needs revision----reached the start + } + Person neigh = dfsStack.pop(); // w = neigh + Person base = dfsStack.peek(); //v = base + if(base.dfs>neigh.back){ // condition for changing base.back + base.back = Math.min(base.back, neigh.back); + } + if(base.dfs <= neigh.back){ //CONNECTOR -- wrong: && base != dfsStart + if(base != dfsStart){ //normal connector + conn[base.zooIndex] = true; + }else{ //connector is at the start + Friendex stPtr = base.front; + while(stPtr != null){ //check for unvisited friends + if(!zoo[stPtr.friendNum].visited){ //if there is an unvisited friend + conn[base.zooIndex] = true; //then the start is a connector + break; + } + stPtr = stPtr.next; + } + } + + } + } + } + } + } + //give ans the all names of the connectors + String ans = ""; + for(int j=0; j friendList; + public Friendex front; + public boolean visited; + public int zooIndex; + public int schoolIndex; + public int back; + public int dfs; - public Person(String name, String school){ //ArrayList friendList + public Person(String name, String school, Friendex front, boolean visited, + int zooIndex, int schoolIndex, int back, int dfs){ //ArrayList friendList this.name = name; this.school = school; - //this.friendList = friendList; + this.front= front; + this.visited = visited; + this.zooIndex = zooIndex; + this.schoolIndex = schoolIndex; + this.back = back; + this.dfs = dfs; } } - -class friendList{ - Person person; - friendList next; - - public friendList(String name, String school, friendList next){ - person = new Person(name, school); - this.next = next; - } -}