Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import xml.Node;
- import xml.NodeId;
- import xml.Path;
- // TODO documentation
- // TODO possibly auxiliary classes
- public class Graph {
- // TODO fields
- // TODO methods
- HashMap<Node, HashMap<Path, Integer>> nodeMap;
- /** Create an empty graph. */
- public Graph() {
- nodeMap = new HashMap<Node, HashMap<Path, Integer>>();
- }
- public Node nodeFetch(String id){
- for(Node n : nodeMap.keySet()){
- if(n.id == id) return n;
- }
- return null;
- }
- /** Create a graph from an XML-parsed graph. */
- public Graph (xml.Graph graph) {
- this();
- addGraph(graph);
- }
- /** Add all nodes and paths from an xml.Graph. */
- public void addGraph(xml.Graph graph) {
- for (Node node : graph.nodes) addNode(node);
- for (Path path : graph.paths) addPath(path);
- }
- /** Add a node. If already present, do nothing. */
- public void addNode(Node node) {
- // TODO
- boolean isPresent = false;
- for(Node k : nodeMap.keySet()){
- if(k.id == node.id) isPresent = true;
- }
- if(isPresent) nodeMap.put(node, null);
- }
- /** Add an undirected edge (called path). The nodes must be present already. */
- public void addPath(Path path) {
- // TODO
- boolean isSourcePresent = false;
- boolean isTargetPresent = false;
- for(Node k : nodeMap.keySet()){
- if(k.id == path.source.id) isSourcePresent = true;
- if(k.id == path.target.id) isTargetPresent = true;
- }
- if(isSourcePresent && isTargetPresent){
- HashMap<Path, Integer> pathMap = nodeMap.get(nodeFetch(path.source.id));
- pathMap.put(path, path.weight);
- nodeMap.put(nodeFetch(path.source.id), pathMap);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement