Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package sjsu.cs146.graph;
- import java.util.ArrayList;
- public class Graph
- {
- private static class OutEdge
- {
- public OutEdge()//(int dest, int theWeight)
- {
- //destination = dest;
- //weight = theWeight;
- }
- private int destination;
- private int weight;
- }
- public Graph(ArrayList<String> list)
- {
- graphList = new ArrayList<ArrayList<OutEdge>>();//(list.size());
- ///
- ///
- for (int i = 0; i < list.size(); i++)
- {
- graphList.add(new ArrayList<OutEdge>());
- String s = list.get(i);
- if (s == "")
- { continue; }
- String[] newS = s.split(" ");
- int sLength = newS.length;
- for (int j = 0; j < (sLength * 0.5); j++)
- { graphList.get(i).add(new OutEdge()); }
- for (int k = 0; k < sLength; k = k+2)
- {
- int tempDest = Integer.parseInt(newS[k]);
- int tempWeight = Integer.parseInt(newS[k+1]);
- graphList.get(i).get(k/2).destination = tempDest;
- graphList.get(i).get(k/2).weight = tempWeight;
- }
- }
- }
- public ArrayList<String> getOutgoingGraphRepresentation()
- {
- for (int i = 0; i < graphList.size(); i++)
- {
- for (int j = 0; j < graphList.get(i).size() - 1; j++)
- {
- for (int k = graphList.get(i).size() - 1; k > j + 1; k--)
- {
- if (graphList.get(i).get(k).destination < graphList.get(i).get(k - 1).destination)
- {
- OutEdge temp = graphList.get(i).get(k);
- graphList.get(i).set(k, graphList.get(i).get(k - 1));
- graphList.get(i).set(k - 1, temp);
- }
- }
- }
- }
- ArrayList<String> sorted = new ArrayList<String>();
- for (int i = 0; i < graphList.size(); i++)
- {
- String tempString = "";
- for (int j = 0; j < graphList.get(i).size(); j++)
- {
- if (j == graphList.get(i).size() - 1)
- {
- tempString = tempString + graphList.get(i).get(j).destination + " ";
- tempString = tempString + graphList.get(i).get(j).weight;
- }
- else
- {
- tempString = tempString + graphList.get(i).get(j).destination + " ";
- tempString = tempString + graphList.get(i).get(j).weight + " ";
- }
- }
- sorted.add(tempString);
- }
- return sorted;////
- }
- public ArrayList<String> getIncomingGraphRepresentation()
- {
- ArrayList<String> sorted = new ArrayList<String>();
- for (int i = 0; i < graphList.size(); i++)
- {
- String tempString = "";
- for (int j = 0; j < graphList.size(); j++)
- {
- for (int k = 0; k < graphList.get(j).size(); k++)
- {
- /*
- if (tempString != "")
- {
- tempString = tempString + " ";
- }
- */
- if (graphList.get(j).get(k).destination == i)
- {
- if (tempString != "")
- { tempString = tempString + " "; }
- tempString = tempString + j + " ";
- //tempString = tempString + graphList.get(j).get(k).destination + " ";
- tempString = tempString + graphList.get(j).get(k).weight;
- }
- }
- }
- sorted.add(tempString);
- }
- return sorted;////
- }
- public boolean hasEdge(int source, int desti)
- {
- ArrayList<OutEdge> start = graphList.get(source);
- for (int i = 0; i < start.size(); i++)
- {
- if (start.get(i).destination == desti)
- { return true; }
- }
- return false;////
- }
- public int getEdgeWeight(int source, int desti)
- {
- ArrayList<OutEdge> start = graphList.get(source);
- for (int i = 0; i < start.size(); i++)
- {
- if (start.get(i).destination == desti)
- { return start.get(i).weight; }
- }
- return 0;
- }
- public int inDegree(int vertexIndex)
- {
- int theDegree = 0;
- for (int j = 0; j < graphList.size(); j++)
- {
- for (int k = 0; k < graphList.get(j).size(); k++)
- {
- if (graphList.get(j).get(k).destination == vertexIndex)
- {
- theDegree++;
- }
- }
- }
- return theDegree;////
- }
- public int outDegree(int vertexIndex)
- {
- int degree = graphList.get(vertexIndex).size();
- return degree;////
- }
- private ArrayList<ArrayList<OutEdge>> graphList;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement