Created
October 21, 2015 14:31
-
-
Save fdoyle/54a0828433c63d05f3eb to your computer and use it in GitHub Desktop.
first stab at a graph deep copy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Main { | |
public static void main(String[] args) { | |
System.out.println("Hello World"); | |
Node a = new Node("A","A"); | |
Node b = new Node("B","B"); | |
Node c = new Node("C","C"); | |
Node d = new Node("D","D"); | |
Node e = new Node("E","E"); | |
Node f = new Node("F","F"); | |
Node g = new Node("G","G"); | |
Node h = new Node("H","H"); | |
a.setConnectedNodes(Arrays.asList(b,a,g,h,e)); | |
b.setConnectedNodes(Arrays.asList(a)); | |
c.setConnectedNodes(Arrays.asList(a,g,h)); | |
d.setConnectedNodes(Arrays.asList(a,e,f,g)); | |
e.setConnectedNodes(Arrays.asList(c,d,e)); | |
f.setConnectedNodes(Arrays.asList(a,e,h)); | |
g.setConnectedNodes(Arrays.asList(d,e,f,g)); | |
h.setConnectedNodes(Arrays.asList(h,g)); | |
a.printToConsole(new HashSet<Node>()); | |
Node copy = a.copy(new HashMap<String, Node>()); | |
copy.printToConsole(new HashSet<Node>()); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Created by fdoyle on 10/20/15. | |
* saw an interview problem posted on the internet, wanted to solve it. here's a solution. | |
* | |
* This is a fresh attempt, never looked at a solution before. | |
* | |
* probably still needs optimization, but i don't think it's doing a ton of extra work. | |
* | |
* Good problem, A+ would solve again. | |
*/ | |
public class Node { | |
public final String identity; | |
public final String label; | |
public List<Node> connectedNodes; | |
public Node(String identity, String label) { | |
this.identity = identity; | |
this.label = label; | |
} | |
public String getIdentity() { | |
return identity; | |
} | |
public List<Node> getConnectedNodes() { | |
return connectedNodes; | |
} | |
public void setConnectedNodes(List<Node> connectedNodes) { | |
this.connectedNodes = connectedNodes; | |
} | |
public Node copy(Map<String, Node> newGraphSet) { | |
Node copiedThis = new Node(this.identity, "son of " + this.label); | |
newGraphSet.put(identity, copiedThis); | |
List<Node> nodesToAdd = new ArrayList<Node>(); | |
for(Node connectedNode : connectedNodes) { | |
if(!newGraphSet.containsKey(connectedNode.identity)) { | |
Node newConnectedNode = connectedNode.copy(newGraphSet); | |
nodesToAdd.add(newConnectedNode); | |
} else { | |
nodesToAdd.add(newGraphSet.get(connectedNode.identity)); | |
} | |
} | |
copiedThis.setConnectedNodes(nodesToAdd); | |
return copiedThis; | |
} | |
public void printToConsole(Set<Node> nodeSet) { | |
if(nodeSet.contains(this)){ | |
return; | |
} else { | |
nodeSet.add(this); | |
} | |
System.out.println(toString()); | |
for(Node node : connectedNodes) { | |
System.out.println(" to:" + node.toString()); | |
} | |
for(Node node : connectedNodes) { | |
node.printToConsole(nodeSet); | |
} | |
} | |
@Override | |
public String toString() { | |
return identity + " " + label; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
also, recursion is the bees knees.