Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.HashSet;
- import java.util.Iterator;
- import org.graphstream.graph.Edge;
- import org.graphstream.graph.Node;
- import org.graphstream.graph.implementations.MultiGraph;
- public class SFG {
- private ArrayList<Boolean> visited;
- private ArrayList<Pair <ArrayList<Node>,Integer>> fwPaths;
- private HashSet<Pair <ArrayList<Node>,Integer>> loops;
- //private DFS pathCalculator;
- public SFG (){
- visited = new ArrayList<Boolean>();
- fwPaths = new ArrayList<Pair<ArrayList<Node>,Integer>>();
- loops = new HashSet<Pair<ArrayList<Node>,Integer>>();
- //pathCalculator = new DFS();
- }
- public void calculatePathsandLoops (MultiGraph graph){
- fwPaths.clear();
- loops.clear();
- visited.clear();
- Node dis= null;
- for(Node n:graph.getEachNode()) {
- visited.add(false);
- dis = n;
- }
- ArrayList<Node> currentPath = new ArrayList<Node>();
- Integer gain = new Integer(1);
- Pair<ArrayList<Node>,Integer> path = new Pair <ArrayList<Node>,Integer> (currentPath,gain);
- traverseGraph (graph,visited, graph.getNode(0),dis,path);
- }
- private void traverseGraph (MultiGraph graph, ArrayList<Boolean> visited,Node current, Node des,Pair<ArrayList<Node>,Integer> path){
- if (current == des){
- path.getLeft().add(des);
- /*printPath(path.getLeft());
- System.out.println(path.getRight());*/
- fwPaths.add(path.clone());
- path.getLeft().remove(current);
- return;
- }
- else {
- visited.set(current.getIndex(),true);
- path.getLeft().add(current);
- for (Edge e : current.getEachLeavingEdge()){
- Node two = e.getNode1();
- if (visited.get(two.getIndex())){
- //System.out.println(current.getId()+" "+two.getId());
- int index = path.getLeft().indexOf(two);
- ArrayList <Node> currentLoop =new ArrayList<Node>(path.getLeft().subList(index,path.getLeft().size()));
- currentLoop.add(two);
- /* gain2 to be edited */
- Integer gain2 = new Integer(1);
- loops.add(new Pair<ArrayList<Node>,Integer>(currentLoop,gain2));
- }
- else{
- Integer gain = path.getRight() * (Integer) e.getAttribute("gain");
- path.setRight(gain);
- traverseGraph (graph,visited,two,des,path);
- gain = path.getRight() / (Integer) e.getAttribute("gain");
- path.setRight(gain);
- }
- }
- visited.set(current.getIndex(),false);
- path.getLeft().remove(current);
- }
- }
- public void printPath (ArrayList<Node> path){
- for (Node n : path){
- System.out.print(n.getId());
- }
- System.out.print("\n");
- }
- public void printLoops (){
- Iterator<Pair<ArrayList<Node>,Integer>> it = loops.iterator();
- while (it.hasNext()){
- Pair<ArrayList<Node>,Integer> temp = it.next();
- printPath(temp.getLeft());
- System.out.println(temp.getRight());
- }
- System.out.println(loops.size());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement