Advertisement
What_Ever

Untitled

Apr 20th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.40 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.HashSet;
  3. import java.util.Iterator;
  4.  
  5. import org.graphstream.graph.Edge;
  6. import org.graphstream.graph.Node;
  7. import org.graphstream.graph.implementations.MultiGraph;
  8.  
  9. public class SFG {
  10.  
  11.    
  12.     private ArrayList<Boolean> visited;
  13.     private ArrayList<Pair <ArrayList<Node>,Integer>> fwPaths;
  14.     private HashSet<Pair <ArrayList<Node>,Integer>> loops;
  15.     //private DFS pathCalculator;
  16.    
  17.     public SFG (){
  18.         visited = new ArrayList<Boolean>();
  19.         fwPaths = new ArrayList<Pair<ArrayList<Node>,Integer>>();
  20.         loops = new HashSet<Pair<ArrayList<Node>,Integer>>();
  21.         //pathCalculator = new DFS();
  22.     }
  23.    
  24.     public void calculatePathsandLoops (MultiGraph graph){
  25.         fwPaths.clear();
  26.         loops.clear();
  27.         visited.clear();
  28.         Node dis= null;
  29.         for(Node n:graph.getEachNode()) {
  30.             visited.add(false);
  31.             dis = n;
  32.         }
  33.         ArrayList<Node> currentPath = new ArrayList<Node>();
  34.         Integer gain = new Integer(1);
  35.         Pair<ArrayList<Node>,Integer> path = new Pair <ArrayList<Node>,Integer> (currentPath,gain);
  36.         traverseGraph (graph,visited, graph.getNode(0),dis,path);
  37.     }
  38.    
  39.     private void traverseGraph (MultiGraph graph, ArrayList<Boolean> visited,Node current, Node des,Pair<ArrayList<Node>,Integer> path){
  40.        
  41.         if (current == des){
  42.             path.getLeft().add(des);
  43.             /*printPath(path.getLeft());
  44.             System.out.println(path.getRight());*/
  45.             fwPaths.add(path.clone());
  46.             path.getLeft().remove(current);
  47.             return;
  48.         }
  49.         else {
  50.            visited.set(current.getIndex(),true);
  51.            path.getLeft().add(current);
  52.            for (Edge e : current.getEachLeavingEdge()){
  53.                Node two = e.getNode1();
  54.                if (visited.get(two.getIndex())){
  55.                    //System.out.println(current.getId()+" "+two.getId());
  56.                   int index = path.getLeft().indexOf(two);
  57.                   ArrayList <Node> currentLoop =new ArrayList<Node>(path.getLeft().subList(index,path.getLeft().size()));
  58.                   currentLoop.add(two);
  59.                   /* gain2 to be edited */
  60.                   Integer gain2 = new Integer(1);
  61.                          
  62.                   loops.add(new Pair<ArrayList<Node>,Integer>(currentLoop,gain2));
  63.                }
  64.                else{
  65.                   Integer gain = path.getRight() * (Integer) e.getAttribute("gain");
  66.                   path.setRight(gain);
  67.                   traverseGraph (graph,visited,two,des,path);
  68.                   gain = path.getRight() / (Integer) e.getAttribute("gain");
  69.                   path.setRight(gain);
  70.                }          
  71.            }
  72.            visited.set(current.getIndex(),false);
  73.            path.getLeft().remove(current);
  74.         }
  75.     }
  76.    
  77.     public void printPath (ArrayList<Node> path){
  78.         for (Node n : path){
  79.             System.out.print(n.getId());
  80.         }
  81.         System.out.print("\n");
  82.     }
  83.    
  84.     public void printLoops (){
  85.         Iterator<Pair<ArrayList<Node>,Integer>> it = loops.iterator();
  86.         while (it.hasNext()){
  87.             Pair<ArrayList<Node>,Integer> temp = it.next();
  88.             printPath(temp.getLeft());
  89.             System.out.println(temp.getRight());
  90.         }
  91.         System.out.println(loops.size());
  92.     }
  93.    
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement