Presentation is loading. Please wait.

Presentation is loading. Please wait.

Graphs and Cycles.

Similar presentations


Presentation on theme: "Graphs and Cycles."— Presentation transcript:

1 Graphs and Cycles

2 IGraph Interface

3 Node Class If we’re trying to see if this node has a route to itself
Use this to connect two nodes Handling cycles Else add this node to the list of ones we’ve seen, then loop through the list of nodes you can reach from this current node, and recurse.

4 Graph Class

5 Examples

6 Let’s walk through her code with one of her examples
Let’s walk through her code with one of her examples. She has: Boston -> Providence -> Hartford bost = this.G.newNode("Boston"); hart = this.G.newNode("Hartford"); prov = this.G.newNode("Providence"); G.addDirectedEdge(bost,prov); G.addDirectedEdge(prov,hart);

7 class Node { private String cityname; // name of city at this node private LinkedList<Node> getsTo; // edges from this Node // constructor only takes the cityname as an argument, // initializing the getsTo list internally Node(String cityname) { this.cityname = cityname; this.getsTo = new LinkedList<Node>(); } // adds an edge from this node to the given toNode public void addEdge(Node toNode) { this.getsTo.add(toNode); // determines whether there is a route from this Node // to the given node boolean hasRoute(Node to, LinkedList<Node> visited) { if (this.equals(to)) return true; if (visited.contains(this)) return false; else { visited.add(this); for (Node c : this.getsTo) { if (c.hasRoute(to,visited)) { Example: bost = this.G.newNode("Boston"); hart = this.G.newNode("Hartford"); prov = this.G.newNode("Providence"); G.addDirectedEdge(bost,prov); G.addDirectedEdge(prov,hart); boolean testbh (Tester t) { return t.checkExpect(G.hasRoute(bost,hart), true); }

8 class Node { private String cityname; // name of city at this node private LinkedList<Node> getsTo; // edges from this Node // constructor only takes the cityname as an argument, // initializing the getsTo list internally Node(String cityname) { this.cityname = cityname; this.getsTo = new LinkedList<Node>(); } // adds an edge from this node to the given toNode public void addEdge(Node toNode) { this.getsTo.add(toNode); // determines whether there is a route from this Node // to the given node boolean hasRoute(Node to, LinkedList<Node> visited) { if (this.equals(to)) return true; if (visited.contains(this)) return false; else { visited.add(this); for (Node c : this.getsTo) { if (c.hasRoute(to,visited)) { Example: bost = this.G.newNode("Boston"); hart = this.G.newNode("Hartford"); prov = this.G.newNode("Providence"); G.addDirectedEdge(bost,prov); G.addDirectedEdge(prov,hart); boolean testbh (Tester t) { return t.checkExpect(G.hasRoute(bost,hart), true); } Graph’s hasRoute method just does: public boolean hasRoute(Node from, Node to) { return from.hasRoute(to, new LinkedList<Node>()); } (Calls the hasRoute method in Node class)

9 class Node { private String cityname; // name of city at this node private LinkedList<Node> getsTo; // edges from this Node // constructor only takes the cityname as an argument, // initializing the getsTo list internally Node(String cityname) { this.cityname = cityname; this.getsTo = new LinkedList<Node>(); } // adds an edge from this node to the given toNode public void addEdge(Node toNode) { this.getsTo.add(toNode); // determines whether there is a route from this Node // to the given node boolean hasRoute(Node to, LinkedList<Node> visited) { if (this.equals(to)) return true; if (visited.contains(this)) return false; else { visited.add(this); for (Node c : this.getsTo) { if (c.hasRoute(to,visited)) { Example: bost = this.G.newNode("Boston"); hart = this.G.newNode("Hartford"); prov = this.G.newNode("Providence"); G.addDirectedEdge(bost,prov); G.addDirectedEdge(prov,hart); boolean testbh (Tester t) { return t.checkExpect(G.hasRoute(bost,hart), true); } Current Values: this = bost (Our node called “bost,” which is Boston) to = hart (Our node called “hart,” which is Hartford) visited = new LinkedList<Node>(); Step We’re On: We just called the method, so we’re just getting started.

10 class Node { private String cityname; // name of city at this node private LinkedList<Node> getsTo; // edges from this Node // constructor only takes the cityname as an argument, // initializing the getsTo list internally Node(String cityname) { this.cityname = cityname; this.getsTo = new LinkedList<Node>(); } // adds an edge from this node to the given toNode public void addEdge(Node toNode) { this.getsTo.add(toNode); // determines whether there is a route from this Node // to the given node boolean hasRoute(Node to, LinkedList<Node> visited) { if (this.equals(to)) return true; if (visited.contains(this)) return false; else { visited.add(this); for (Node c : this.getsTo) { if (c.hasRoute(to,visited)) { Example: bost = this.G.newNode("Boston"); hart = this.G.newNode("Hartford"); prov = this.G.newNode("Providence"); G.addDirectedEdge(bost,prov); G.addDirectedEdge(prov,hart); boolean testbh (Tester t) { return t.checkExpect(G.hasRoute(bost,hart), true); } Current Values: this = bost (Our node called “bost,” which is Boston) to = hart (Our node called “hart,” which is Hartford) visited = new LinkedList<Node>(); Step We’re On: Is the node we’re trying to get to the node we’re currently at?

11 class Node { private String cityname; // name of city at this node private LinkedList<Node> getsTo; // edges from this Node // constructor only takes the cityname as an argument, // initializing the getsTo list internally Node(String cityname) { this.cityname = cityname; this.getsTo = new LinkedList<Node>(); } // adds an edge from this node to the given toNode public void addEdge(Node toNode) { this.getsTo.add(toNode); // determines whether there is a route from this Node // to the given node boolean hasRoute(Node to, LinkedList<Node> visited) { if (this.equals(to)) return true; if (visited.contains(this)) return false; else { visited.add(this); for (Node c : this.getsTo) { if (c.hasRoute(to,visited)) { Example: bost = this.G.newNode("Boston"); hart = this.G.newNode("Hartford"); prov = this.G.newNode("Providence"); G.addDirectedEdge(bost,prov); G.addDirectedEdge(prov,hart); boolean testbh (Tester t) { return t.checkExpect(G.hasRoute(bost,hart), true); } Current Values: this = bost (Our node called “bost,” which is Boston) to = hart (Our node called “hart,” which is Hartford) visited = new LinkedList<Node>(); Step We’re On: Is the node we’re trying to get to the node we’re currently at? No, it’s not, so we won’t go inside of this if statement.

12 class Node { private String cityname; // name of city at this node private LinkedList<Node> getsTo; // edges from this Node // constructor only takes the cityname as an argument, // initializing the getsTo list internally Node(String cityname) { this.cityname = cityname; this.getsTo = new LinkedList<Node>(); } // adds an edge from this node to the given toNode public void addEdge(Node toNode) { this.getsTo.add(toNode); // determines whether there is a route from this Node // to the given node boolean hasRoute(Node to, LinkedList<Node> visited) { if (this.equals(to)) return true; if (visited.contains(this)) return false; else { visited.add(this); for (Node c : this.getsTo) { if (c.hasRoute(to,visited)) { Example: bost = this.G.newNode("Boston"); hart = this.G.newNode("Hartford"); prov = this.G.newNode("Providence"); G.addDirectedEdge(bost,prov); G.addDirectedEdge(prov,hart); boolean testbh (Tester t) { return t.checkExpect(G.hasRoute(bost,hart), true); } Current Values: this = bost (Our node called “bost,” which is Boston) to = hart (Our node called “hart,” which is Hartford) visited = new LinkedList<Node>(); Step We’re On: Have we visited this node before? No we haven’t, so we won’t go into this if statement either.

13 class Node { private String cityname; // name of city at this node private LinkedList<Node> getsTo; // edges from this Node // constructor only takes the cityname as an argument, // initializing the getsTo list internally Node(String cityname) { this.cityname = cityname; this.getsTo = new LinkedList<Node>(); } // adds an edge from this node to the given toNode public void addEdge(Node toNode) { this.getsTo.add(toNode); // determines whether there is a route from this Node // to the given node boolean hasRoute(Node to, LinkedList<Node> visited) { if (this.equals(to)) return true; if (visited.contains(this)) return false; else { visited.add(this); for (Node c : this.getsTo) { if (c.hasRoute(to,visited)) { Example: bost = this.G.newNode("Boston"); hart = this.G.newNode("Hartford"); prov = this.G.newNode("Providence"); G.addDirectedEdge(bost,prov); G.addDirectedEdge(prov,hart); boolean testbh (Tester t) { return t.checkExpect(G.hasRoute(bost,hart), true); } Current Values: this = bost (Our node called “bost,” which is Boston) to = hart (Our node called “hart,” which is Hartford) visited = bost Step We’re On: We’re now in this else statement. We add this, which is bost, to our visited list. Now we go into the for loop. For each node (each city) in the list of cities that bost is connected to, we recurse. The first (and only) node in bost’s getsTo list is prov, so “c” represents prov , “to” is still hart, and our “visited” now contains bost.

14 class Node { private String cityname; // name of city at this node private LinkedList<Node> getsTo; // edges from this Node // constructor only takes the cityname as an argument, // initializing the getsTo list internally Node(String cityname) { this.cityname = cityname; this.getsTo = new LinkedList<Node>(); } // adds an edge from this node to the given toNode public void addEdge(Node toNode) { this.getsTo.add(toNode); // determines whether there is a route from this Node // to the given node boolean hasRoute(Node to, LinkedList<Node> visited) { if (this.equals(to)) return true; if (visited.contains(this)) return false; else { visited.add(this); for (Node c : this.getsTo) { if (c.hasRoute(to,visited)) { Example: bost = this.G.newNode("Boston"); hart = this.G.newNode("Hartford"); prov = this.G.newNode("Providence"); G.addDirectedEdge(bost,prov); G.addDirectedEdge(prov,hart); boolean testbh (Tester t) { return t.checkExpect(G.hasRoute(bost,hart), true); } Current Values: this = prov (Our node called “prov,” which is Providence) to = hart (Our node called “hart,” which is Hartford) visited = bost Step We’re On: Note how above I’ve changed “this” to be prov. We are now in prov’s hasRoute call. prov != hart, so we don’t go into this if statement.

15 class Node { private String cityname; // name of city at this node private LinkedList<Node> getsTo; // edges from this Node // constructor only takes the cityname as an argument, // initializing the getsTo list internally Node(String cityname) { this.cityname = cityname; this.getsTo = new LinkedList<Node>(); } // adds an edge from this node to the given toNode public void addEdge(Node toNode) { this.getsTo.add(toNode); // determines whether there is a route from this Node // to the given node boolean hasRoute(Node to, LinkedList<Node> visited) { if (this.equals(to)) return true; if (visited.contains(this)) return false; else { visited.add(this); for (Node c : this.getsTo) { if (c.hasRoute(to,visited)) { Example: bost = this.G.newNode("Boston"); hart = this.G.newNode("Hartford"); prov = this.G.newNode("Providence"); G.addDirectedEdge(bost,prov); G.addDirectedEdge(prov,hart); boolean testbh (Tester t) { return t.checkExpect(G.hasRoute(bost,hart), true); } Current Values: this = prov (Our node called “prov,” which is Providence) to = hart (Our node called “hart,” which is Hartford) visited = bost Step We’re On: prov is not in the visited list, so we don’t go into this if statement.

16 class Node { private String cityname; // name of city at this node private LinkedList<Node> getsTo; // edges from this Node // constructor only takes the cityname as an argument, // initializing the getsTo list internally Node(String cityname) { this.cityname = cityname; this.getsTo = new LinkedList<Node>(); } // adds an edge from this node to the given toNode public void addEdge(Node toNode) { this.getsTo.add(toNode); // determines whether there is a route from this Node // to the given node boolean hasRoute(Node to, LinkedList<Node> visited) { if (this.equals(to)) return true; if (visited.contains(this)) return false; else { visited.add(this); for (Node c : this.getsTo) { if (c.hasRoute(to,visited)) { Example: bost = this.G.newNode("Boston"); hart = this.G.newNode("Hartford"); prov = this.G.newNode("Providence"); G.addDirectedEdge(bost,prov); G.addDirectedEdge(prov,hart); boolean testbh (Tester t) { return t.checkExpect(G.hasRoute(bost,hart), true); } Current Values: this = prov (Our node called “prov,” which is Providence) to = hart (Our node called “hart,” which is Hartford) visited = bost Step We’re On: Let’s go into the else statement!

17 class Node { private String cityname; // name of city at this node private LinkedList<Node> getsTo; // edges from this Node // constructor only takes the cityname as an argument, // initializing the getsTo list internally Node(String cityname) { this.cityname = cityname; this.getsTo = new LinkedList<Node>(); } // adds an edge from this node to the given toNode public void addEdge(Node toNode) { this.getsTo.add(toNode); // determines whether there is a route from this Node // to the given node boolean hasRoute(Node to, LinkedList<Node> visited) { if (this.equals(to)) return true; if (visited.contains(this)) return false; else { visited.add(this); for (Node c : this.getsTo) { if (c.hasRoute(to,visited)) { Example: bost = this.G.newNode("Boston"); hart = this.G.newNode("Hartford"); prov = this.G.newNode("Providence"); G.addDirectedEdge(bost,prov); G.addDirectedEdge(prov,hart); boolean testbh (Tester t) { return t.checkExpect(G.hasRoute(bost,hart), true); } Current Values: this = prov (Our node called “prov,” which is Providence) to = hart (Our node called “hart,” which is Hartford) visited = bost, prov Step We’re On: First, we updated our visited list (see above).

18 class Node { private String cityname; // name of city at this node private LinkedList<Node> getsTo; // edges from this Node // constructor only takes the cityname as an argument, // initializing the getsTo list internally Node(String cityname) { this.cityname = cityname; this.getsTo = new LinkedList<Node>(); } // adds an edge from this node to the given toNode public void addEdge(Node toNode) { this.getsTo.add(toNode); // determines whether there is a route from this Node // to the given node boolean hasRoute(Node to, LinkedList<Node> visited) { if (this.equals(to)) return true; if (visited.contains(this)) return false; else { visited.add(this); for (Node c : this.getsTo) { if (c.hasRoute(to,visited)) { Example: bost = this.G.newNode("Boston"); hart = this.G.newNode("Hartford"); prov = this.G.newNode("Providence"); G.addDirectedEdge(bost,prov); G.addDirectedEdge(prov,hart); boolean testbh (Tester t) { return t.checkExpect(G.hasRoute(bost,hart), true); } Current Values: this = prov (Our node called “prov,” which is Providence) to = hart (Our node called “hart,” which is Hartford) visited = bost, prov Step We’re On: Now, for every node (city) in prov’s getsTo list, we loop.

19 class Node { private String cityname; // name of city at this node private LinkedList<Node> getsTo; // edges from this Node // constructor only takes the cityname as an argument, // initializing the getsTo list internally Node(String cityname) { this.cityname = cityname; this.getsTo = new LinkedList<Node>(); } // adds an edge from this node to the given toNode public void addEdge(Node toNode) { this.getsTo.add(toNode); // determines whether there is a route from this Node // to the given node boolean hasRoute(Node to, LinkedList<Node> visited) { if (this.equals(to)) return true; if (visited.contains(this)) return false; else { visited.add(this); for (Node c : this.getsTo) { if (c.hasRoute(to,visited)) { Example: bost = this.G.newNode("Boston"); hart = this.G.newNode("Hartford"); prov = this.G.newNode("Providence"); G.addDirectedEdge(bost,prov); G.addDirectedEdge(prov,hart); boolean testbh (Tester t) { return t.checkExpect(G.hasRoute(bost,hart), true); } Current Values: this = prov (Our node called “prov,” which is Providence) to = hart (Our node called “hart,” which is Hartford) visited = bost, prov Step We’re On: We make our recursive call. “c” is hart, since in our examples we said there is an edge from prov to hart. This means hart is in prov’s getsTo list. “to” is still hart. “visited” contains bost and prov.

20 class Node { private String cityname; // name of city at this node private LinkedList<Node> getsTo; // edges from this Node // constructor only takes the cityname as an argument, // initializing the getsTo list internally Node(String cityname) { this.cityname = cityname; this.getsTo = new LinkedList<Node>(); } // adds an edge from this node to the given toNode public void addEdge(Node toNode) { this.getsTo.add(toNode); // determines whether there is a route from this Node // to the given node boolean hasRoute(Node to, LinkedList<Node> visited) { if (this.equals(to)) return true; if (visited.contains(this)) return false; else { visited.add(this); for (Node c : this.getsTo) { if (c.hasRoute(to,visited)) { Example: bost = this.G.newNode("Boston"); hart = this.G.newNode("Hartford"); prov = this.G.newNode("Providence"); G.addDirectedEdge(bost,prov); G.addDirectedEdge(prov,hart); boolean testbh (Tester t) { return t.checkExpect(G.hasRoute(bost,hart), true); } Current Values: this = hart to = hart (Our node called “hart,” which is Hartford) visited = bost, prov Step We’re On: We made our recursive call. See the updated variables above.

21 class Node { private String cityname; // name of city at this node private LinkedList<Node> getsTo; // edges from this Node // constructor only takes the cityname as an argument, // initializing the getsTo list internally Node(String cityname) { this.cityname = cityname; this.getsTo = new LinkedList<Node>(); } // adds an edge from this node to the given toNode public void addEdge(Node toNode) { this.getsTo.add(toNode); // determines whether there is a route from this Node // to the given node boolean hasRoute(Node to, LinkedList<Node> visited) { if (this.equals(to)) return true; if (visited.contains(this)) return false; else { visited.add(this); for (Node c : this.getsTo) { if (c.hasRoute(to,visited)) { Example: bost = this.G.newNode("Boston"); hart = this.G.newNode("Hartford"); prov = this.G.newNode("Providence"); G.addDirectedEdge(bost,prov); G.addDirectedEdge(prov,hart); boolean testbh (Tester t) { return t.checkExpect(G.hasRoute(bost,hart), true); } Current Values: this = hart to = hart (Our node called “hart,” which is Hartford) visited = bost, prov Step We’re On: this DOES equal to, so we return true! This method call is done.

22 class Node { private String cityname; // name of city at this node private LinkedList<Node> getsTo; // edges from this Node // constructor only takes the cityname as an argument, // initializing the getsTo list internally Node(String cityname) { this.cityname = cityname; this.getsTo = new LinkedList<Node>(); } // adds an edge from this node to the given toNode public void addEdge(Node toNode) { this.getsTo.add(toNode); // determines whether there is a route from this Node // to the given node boolean hasRoute(Node to, LinkedList<Node> visited) { if (this.equals(to)) return true; if (visited.contains(this)) return false; else { visited.add(this); for (Node c : this.getsTo) { if (c.hasRoute(to,visited)) { Example: bost = this.G.newNode("Boston"); hart = this.G.newNode("Hartford"); prov = this.G.newNode("Providence"); G.addDirectedEdge(bost,prov); G.addDirectedEdge(prov,hart); boolean testbh (Tester t) { return t.checkExpect(G.hasRoute(bost,hart), true); } Current Values: this = prov (Our node called “prov,” which is Providence) to = hart (Our node called “hart,” which is Hartford) visited = bost, prov Step We’re On: Since this is recursion, we now go back to the “hasRoute” call was made on prov. Remember how we checked the line in red? Well that line returned true! Since it returned true, we can go into this if statement, which also returns true. This means our hasRoute method that we called on prov , one of the nodes in bost’s getsTo list, has returned true.

23 class Node { private String cityname; // name of city at this node private LinkedList<Node> getsTo; // edges from this Node // constructor only takes the cityname as an argument, // initializing the getsTo list internally Node(String cityname) { this.cityname = cityname; this.getsTo = new LinkedList<Node>(); } // adds an edge from this node to the given toNode public void addEdge(Node toNode) { this.getsTo.add(toNode); // determines whether there is a route from this Node // to the given node boolean hasRoute(Node to, LinkedList<Node> visited) { if (this.equals(to)) return true; if (visited.contains(this)) return false; else { visited.add(this); for (Node c : this.getsTo) { if (c.hasRoute(to,visited)) { Example: bost = this.G.newNode("Boston"); hart = this.G.newNode("Hartford"); prov = this.G.newNode("Providence"); G.addDirectedEdge(bost,prov); G.addDirectedEdge(prov,hart); boolean testbh (Tester t) { return t.checkExpect(G.hasRoute(bost,hart), true); } Current Values: this = bost (Our node called “bost,” which is Boston) to = hart (Our node called “hart,” which is Hartford) visited = bost Step We’re On: We now go back to the method call “hasRoute” which was called on the node bost. See the variables above. Since the c.hasRoute(to,visited) call returned true in prov, this statement then returns true! So this whole hasRoute method has returned true. There is a route from Boston to Hartford.

24 class Node { private String cityname; // name of city at this node private LinkedList<Node> getsTo; // edges from this Node // constructor only takes the cityname as an argument, // initializing the getsTo list internally Node(String cityname) { this.cityname = cityname; this.getsTo = new LinkedList<Node>(); } // adds an edge from this node to the given toNode public void addEdge(Node toNode) { this.getsTo.add(toNode); // determines whether there is a route from this Node // to the given node boolean hasRoute(Node to, LinkedList<Node> visited) { if (this.equals(to)) return true; if (visited.contains(this)) return false; else { visited.add(this); for (Node c : this.getsTo) { if (c.hasRoute(to,visited)) { Example: bost = this.G.newNode("Boston"); hart = this.G.newNode("Hartford"); prov = this.G.newNode("Providence"); G.addDirectedEdge(bost,prov); G.addDirectedEdge(prov,hart); boolean testbh (Tester t) { return t.checkExpect(G.hasRoute(bost,hart), true); } Just as a note: If there was not a route from Boston to Hartford, this first for loop would have looped through all cities connected to Boston, returned false for each recursive call, and finally would have ended up in this else statement.


Download ppt "Graphs and Cycles."

Similar presentations


Ads by Google