Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.91 KB | None | 0 0
  1. package sjsu.cs146.graph;
  2.  
  3. import java.util.ArrayList;
  4.  
  5.  
  6. public class Graph
  7. {
  8.     private static class OutEdge
  9.     {
  10.         public OutEdge()//(int dest, int theWeight)
  11.         {
  12.             //destination = dest;
  13.             //weight = theWeight;
  14.         }
  15.        
  16.         private int destination;
  17.         private int weight;
  18.     }
  19.    
  20.     public Graph(ArrayList<String> list)
  21.     {
  22.         graphList = new ArrayList<ArrayList<OutEdge>>();//(list.size());
  23.         ///
  24.         ///
  25.        
  26.         for (int i = 0; i < list.size(); i++)
  27.         {
  28.             graphList.add(new ArrayList<OutEdge>());
  29.             String s = list.get(i);
  30.             if (s == "")
  31.             { continue; }
  32.             String[] newS = s.split(" ");
  33.             int sLength = newS.length;
  34.            
  35.             for (int j = 0; j < (sLength * 0.5); j++)
  36.             { graphList.get(i).add(new OutEdge()); }
  37.            
  38.             for (int k = 0; k < sLength; k = k+2)
  39.             {
  40.                 int tempDest = Integer.parseInt(newS[k]);
  41.                 int tempWeight = Integer.parseInt(newS[k+1]);
  42.                 graphList.get(i).get(k/2).destination = tempDest;
  43.                 graphList.get(i).get(k/2).weight = tempWeight;
  44.             }
  45.            
  46.         }
  47.     }
  48.    
  49.     public ArrayList<String> getOutgoingGraphRepresentation()
  50.     {
  51.         for (int i = 0; i < graphList.size(); i++)
  52.         {
  53.             for (int j = 0; j < graphList.get(i).size() - 1; j++)
  54.             {
  55.                 for (int k = graphList.get(i).size() - 1; k > j + 1; k--)
  56.                 {
  57.                     if (graphList.get(i).get(k).destination < graphList.get(i).get(k - 1).destination)
  58.                     {
  59.                         OutEdge temp = graphList.get(i).get(k);
  60.                         graphList.get(i).set(k, graphList.get(i).get(k - 1));
  61.                         graphList.get(i).set(k - 1, temp);
  62.                     }
  63.                 }
  64.             }
  65.         }
  66.        
  67.         ArrayList<String> sorted = new ArrayList<String>();
  68.         for (int i = 0; i < graphList.size(); i++)
  69.         {
  70.             String tempString = "";
  71.             for (int j = 0; j < graphList.get(i).size(); j++)
  72.             {
  73.                 if (j == graphList.get(i).size() - 1)
  74.                 {
  75.                     tempString = tempString + graphList.get(i).get(j).destination + " ";
  76.                     tempString = tempString + graphList.get(i).get(j).weight;
  77.                 }
  78.                 else
  79.                 {
  80.                     tempString = tempString + graphList.get(i).get(j).destination + " ";
  81.                     tempString = tempString + graphList.get(i).get(j).weight + " ";
  82.                 }
  83.             }
  84.             sorted.add(tempString);
  85.         }
  86.        
  87.         return sorted;////
  88.     }
  89.    
  90.     public ArrayList<String> getIncomingGraphRepresentation()
  91.     {
  92.         ArrayList<String> sorted = new ArrayList<String>();
  93.         for (int i = 0; i < graphList.size(); i++)
  94.         {
  95.             String tempString = "";
  96.             for (int j = 0; j < graphList.size(); j++)
  97.             {
  98.                 for (int k = 0; k < graphList.get(j).size(); k++)
  99.                 {
  100.                     /*
  101.                     if (tempString != "")
  102.                     {
  103.                         tempString = tempString + " ";
  104.                     }
  105.                     */
  106.                     if (graphList.get(j).get(k).destination == i)
  107.                     {
  108.                         if (tempString != "")
  109.                         { tempString = tempString + " "; }
  110.                        
  111.                         tempString = tempString + j + " ";
  112.                         //tempString = tempString + graphList.get(j).get(k).destination + " ";
  113.                         tempString = tempString + graphList.get(j).get(k).weight;
  114.                     }
  115.                 }
  116.             }
  117.             sorted.add(tempString);
  118.         }
  119.        
  120.         return sorted;////
  121.     }
  122.    
  123.     public boolean hasEdge(int source, int desti)
  124.     {
  125.         ArrayList<OutEdge> start = graphList.get(source);
  126.         for (int i = 0; i < start.size(); i++)
  127.         {
  128.             if (start.get(i).destination == desti)
  129.             { return true; }
  130.         }
  131.         return false;////
  132.     }
  133.    
  134.     public int getEdgeWeight(int source, int desti)
  135.     {
  136.         ArrayList<OutEdge> start = graphList.get(source);
  137.         for (int i = 0; i < start.size(); i++)
  138.         {
  139.             if (start.get(i).destination == desti)
  140.             { return start.get(i).weight; }
  141.         }
  142.        
  143.         return 0;
  144.     }
  145.    
  146.     public int inDegree(int vertexIndex)
  147.     {
  148.         int theDegree = 0;
  149.        
  150.         for (int j = 0; j < graphList.size(); j++)
  151.         {
  152.             for (int k = 0; k < graphList.get(j).size(); k++)
  153.             {
  154.                 if (graphList.get(j).get(k).destination == vertexIndex)
  155.                 {
  156.                     theDegree++;
  157.                 }
  158.             }
  159.         }
  160.        
  161.         return theDegree;////
  162.     }
  163.    
  164.     public int outDegree(int vertexIndex)
  165.     {
  166.         int degree = graphList.get(vertexIndex).size();
  167.         return degree;////
  168.     }
  169.    
  170.     private ArrayList<ArrayList<OutEdge>> graphList;
  171.  
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement