250 likes | 265 Views
Lists (and other data structures) that Loop. CS1316: Representing Structure and Behavior. Story. Cell animation An example of when it’s useful to have a list reference itself. Graphs: Trees that reference themselves. Characters from MediaSources. gal1-rightface.jpg. gal1-right2.jpg.
E N D
Lists (and other data structures) that Loop CS1316: Representing Structure and Behavior
Story • Cell animation • An example of when it’s useful to have a list reference itself. • Graphs: Trees that reference themselves
Characters from MediaSources gal1-rightface.jpg gal1-right2.jpg gal1-right1.jpg
Walking (roughly, like Frankenstein) gal1-rightface.jpg gal1-rightface.jpg gal1-rightface.jpg gal1-right1.jpg gal1-right2.jpg
As a Circular Linked List gal1-rightface.jpg gal1-rightface.jpg gal1-right1.jpg gal1-right2.jpg
Can we do this? • Sure! Why not? • There are lots of real information in the world that “cycles” or “loops” or “connects” • Maps of roads • Electrical diagrams • Pipe diagrams • Subway routes • The trick is Don’t try to traverse the list!
Creating a WalkingDoll > WalkingDoll gal = new WalkingDoll(); gal.setUp(); > gal.steps(10);
WalkingDoll class public class WalkingDoll { /** * Which character node position are we at? **/ public CharNode current; /** * Starting position for new walking. **/ public CharNode start; /** * Position for the character **/ public int x, y; public int getX() {return x;} public int getY() {return y;} public void setLoc(int nux, int nuy){x=nux; y=nuy;} /** * FrameSequence for the display **/ FrameSequence frames;
Making theCircular List /** * We'll do the list setup in the constructor **/ public WalkingDoll(){ FileChooser.setMediaPath("D:/cs1316/mediasources/"); Picture p = null; // For loading up images p = new Picture(FileChooser.getMediaPath("gal1-rightface.jpg")); start = new CharNode(p); p = new Picture(FileChooser.getMediaPath("gal1-right2.jpg")); CharNode rightfoot = new CharNode(p); p = new Picture(FileChooser.getMediaPath("gal1-rightface.jpg")); CharNode center = new CharNode(p); p = new Picture(FileChooser.getMediaPath("gal1-right1.jpg")); CharNode leftfoot = new CharNode(p); start.setNext(rightfoot); rightfoot.setNext(center); center.setNext(leftfoot); // Now the scary one leftfoot.setNext(start); frames = new FrameSequence("D:/Temp/"); }
Setting up the walk /** * Setup to display walking left to right **/ public void setUp(){ x = 0; // Left side y = 300; // 300 pixels down frames.show(); this.start(); }
Drawing the Character /** * Start a walking sequence **/ public void start() { current = start; this.draw(); } /** * Draw the current character **/ public void draw() { Picture bg = new Picture(400,400); Turtle pen = new Turtle(bg); pen.setPenDown(false); pen.moveTo(x,y); current.drawWith(pen); frames.addFrame(bg); }
Walking /** * Draw the next step **/ public void step(){ current = (CharNode) current.getNext(); x=x+10; // We'll try this this.draw(); } /** * Draw a few steps **/ public void steps(int num){ for (int i=0; i < num; i++) {this.step();}}
Try it! • How would you walk right-to-left? • Flip the images • Start on the right • Decrement x instead of incrementing it
Try it!Maybe this would be better? gal1-rightface.jpg gal1-right2.jpg Just change the constructor and the rest should work! gal1-right1.jpg
CharNode: A safe picture linked list > CharNode ch = new CharNode(new Picture(20,20)); > ch.last() Don't try to find last() from a circular list! CharNode with picture: Picture, filename null height 20 width 20 > ch.remove(new CharNode(new Picture(20,20))) Very dangerous to try to remove a node from this list! Disable those methods in the LLNode that could cause traversals!
CharNode class /* * CharNode is a class representing a drawn picture * that is one in a sequence of Pictures to * use for a given character. Don't ever try to traverse this one! **/ public class CharNode extends LLNode { /** * The picture I'm associated with **/ public Picture myPict; /* * Make me with this picture * @param pict the Picture I'm associated with **/ public CharNode(Picture pict){ super(); // Call superclass constructor myPict = pict; } Nothing much surprising here.
Preventing bad things from happening /** * Don't try to remove() from a circular list! **/ public void remove(LLNode node){ System.out.println("Very dangerous to try to remove a node from this list!"); } /** * Don't try to get the last() from a circular list! **/ public LLNode last() { System.out.println("Don't try to find last() from a circular list!"); return this; } These override the code in LLNode
NON-recursive toString() /** * Method to return a string with information * about this node */ public String toString() { return "CharNode with picture: "+myPict; }
Just draw as bluescreen /* * Use the given turtle to draw oneself * @param pen the Turtle to draw with **/ public void drawWith(Turtle pen){ // Assume that we're at the lower-left corner pen.setHeading(0); pen.forward(myPict.getHeight()); Picture bg = pen.getPicture(); myPict.bluescreen(bg,pen.getXPos(),pen.getYPos()); }
If we can have lists that loop,can we have trees that loop? • Sure! These are called graphs. • A graph is a series of points (vertices) and edges (lines) that connect them. • If there is a direction to the line, it’s called directed. If not, undirected. • If a graph ever loops (cycles), it’s called cyclic. • There can also be costs associated with edges.
Example Graphs • MARTA subway • It’s an acyclic graph, where vertices are stations and the north-south and east-west lines are the edges. • Cost might be the time between stations. • Interstate system • It’s a cyclic graph where vertices are intersections or exits or cities (depending on how you want to think about it). • Cost is distance between vertices.
What can we do with graphs? • Find all vertices that are reachable from a given vertex. • “Can I get there from here?” • Not always true—think about wiring for a house with a short in it. • Find a spanning tree—an acyclic graph (a tree!) that touches all vertices. • A minimal spanning tree does this while minimizing cost.
Minimal Spanning Tree • http://www.cs.sunysb.edu/~skiena/combinatorica/animations/mst.html • “The minimum spanning tree (MST) of a weighted graph is a spanning tree whose sum of edge weights is minimal. The minimum spanning tree describes the cheapest network to connect all of a given set of vertices. Kruskal's algorithm for minimum spanning tree works by inserting edges in order of increasing cost, adding as edges to the tree those which connect two previously disjoint components. • Here we see Kruskal's algorithm at work on a graph of distances between 128 North American cities. Almost imperceptively at first, short edges get added all around the continent, slowly building larger components until the tree is completed.”