Advertisement
Guest User

Untitled

a guest
Mar 2nd, 2015
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.62 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. import xml.Node;
  4. import xml.NodeId;
  5. import xml.Path;
  6.  
  7. // TODO documentation
  8.  
  9. // TODO possibly auxiliary classes
  10.  
  11. public class Graph {
  12.  
  13.     // TODO fields
  14.     // TODO methods
  15.  
  16.     HashMap<Node, HashMap<Path, Integer>> nodeMap;
  17.  
  18.     /** Create an empty graph. */
  19.     public Graph() {
  20.         nodeMap = new HashMap<Node, HashMap<Path, Integer>>();
  21.     }
  22.  
  23.     public Node nodeFetch(String id){
  24.         for(Node n : nodeMap.keySet()){
  25.             if(n.id == id) return n;
  26.         }
  27.         return null;
  28.     }
  29.  
  30.  
  31.     /** Create a graph from an XML-parsed graph. */
  32.     public Graph (xml.Graph graph) {
  33.         this();
  34.         addGraph(graph);
  35.     }
  36.  
  37.     /** Add all nodes and paths from an xml.Graph. */
  38.     public void addGraph(xml.Graph graph) {
  39.         for (Node node : graph.nodes) addNode(node);
  40.         for (Path path : graph.paths) addPath(path);
  41.     }
  42.  
  43.     /** Add a node. If already present, do nothing. */
  44.     public void addNode(Node node) {
  45.         // TODO
  46.         boolean isPresent = false;
  47.         for(Node k : nodeMap.keySet()){
  48.             if(k.id == node.id) isPresent = true;
  49.         }
  50.         if(isPresent) nodeMap.put(node, null);
  51.     }
  52.  
  53.     /** Add an undirected edge (called path). The nodes must be present already. */
  54.     public void addPath(Path path) {
  55.         // TODO
  56.         boolean isSourcePresent = false;
  57.         boolean isTargetPresent = false;
  58.  
  59.         for(Node k : nodeMap.keySet()){
  60.             if(k.id == path.source.id) isSourcePresent = true;
  61.             if(k.id == path.target.id) isTargetPresent = true;
  62.         }
  63.  
  64.         if(isSourcePresent && isTargetPresent){
  65.             HashMap<Path, Integer> pathMap = nodeMap.get(nodeFetch(path.source.id));
  66.             pathMap.put(path, path.weight);
  67.  
  68.             nodeMap.put(nodeFetch(path.source.id), pathMap);
  69.         }
  70.     }
  71.  
  72.  
  73.  
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement