Lists (and other data structures) that Loop CS1316: Representing Structure and Behavior.

Slides:



Advertisements
Similar presentations
Decision Maths Networks Kruskals Algorithm Wiltshire Networks A Network is a weighted graph, which just means there is a number associated with each.
Advertisements

Lecture 15. Graph Algorithms
Great Theoretical Ideas in Computer Science for Some.
Graphs III (Trees, MSTs) (Chp 11.5, 11.6)
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 21: Graphs.
Chapter 23 Minimum Spanning Trees
Graph Algorithms: Minimum Spanning Tree We are given a weighted, undirected graph G = (V, E), with weight function w:
Spanning Trees.
Spanning Trees. 2 Spanning trees Suppose you have a connected undirected graph Connected: every node is reachable from every other node Undirected: edges.
Introduction to Graphs
Minimum Spanning Trees CIS 606 Spring Problem A town has a set of houses and a set of roads. A road connects 2 and only 2 houses. A road connecting.
Chapter 9: Graphs Basic Concepts
Spanning Trees. Spanning trees Suppose you have a connected undirected graph –Connected: every node is reachable from every other node –Undirected: edges.
Minimum Spanning Trees. Subgraph A graph G is a subgraph of graph H if –The vertices of G are a subset of the vertices of H, and –The edges of G are a.
Minimum Spanning Tree Algorithms. What is A Spanning Tree? u v b a c d e f Given a connected, undirected graph G=(V,E), a spanning tree of that graph.
Problem Solving with Data Structures using Java: A Multimedia Approach Chapter 8: Trees of Images.
Minimum Spanning Tree Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
Minimal Spanning Trees What is a minimal spanning tree (MST) and how to find one.
Midwestern State University Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm 1.
1.1 Data Structure and Algorithm Lecture 13 Minimum Spanning Trees Topics Reference: Introduction to Algorithm by Cormen Chapter 13: Minimum Spanning Trees.
CS 146: Data Structures and Algorithms July 21 Class Meeting
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
Graph Dr. Bernard Chen Ph.D. University of Central Arkansas.
Networks.
Spanning Trees Introduction to Spanning Trees AQR MRS. BANKS Original Source: Prof. Roger Crawfis from Ohio State University.
Spanning Trees Introduction to Spanning Trees AQR MRS. BANKS Original Source: Prof. Roger Crawfis from Ohio State University.
Minimum Spanning Trees and Kruskal’s Algorithm CLRS 23.
Dijkstra’s Algorithm. Announcements Assignment #2 Due Tonight Exams Graded Assignment #3 Posted.
Module 5 – Networks and Decision Mathematics Chapter 23 – Undirected Graphs.
Minimum Spanning Trees Prof. Sin-Min Lee Dept. of Computer Science, San Jose State University.
Graphs. Definitions A graph is two sets. A graph is two sets. –A set of nodes or vertices V –A set of edges E Edges connect nodes. Edges connect nodes.
Fundamental Data Structures and Algorithms (Spring ’05) Recitation Notes: Graphs Slides prepared by Uri Dekel, Based on recitation.
Problem Solving with Data Structures using Java: A Multimedia Approach Chapter 12: Circular Linked Lists And Graphs: Lists and Trees That Loop.
1 12/2/2015 MATH 224 – Discrete Mathematics Formally a graph is just a collection of unordered or ordered pairs, where for example, if {a,b} G if a, b.
Minimum Spanning Trees CS 146 Prof. Sin-Min Lee Regina Wang.
Graphs A ‘Graph’ is a diagram that shows how things are connected together. It makes no attempt to draw actual paths or routes and scale is generally inconsequential.
Finally! Making the Villagers CS1316: Representing Structure and Behavior.
Chapter 20: Graphs. Objectives In this chapter, you will: – Learn about graphs – Become familiar with the basic terminology of graph theory – Discover.
Tree Diagrams A tree is a connected graph in which every edge is a bridge. There can NEVER be a circuit in a tree diagram!
Problem Solving with Data Structures using Java: A Multimedia Approach Chapter 7: Structuring Images Using Linked Lists.
Graphs Definition: a graph is an abstract representation of a set of objects where some pairs of the objects are connected by links. The interconnected.
Graphs. Graph Definitions A graph G is denoted by G = (V, E) where  V is the set of vertices or nodes of the graph  E is the set of edges or arcs connecting.
Graph Concepts Illustrated Using The Leda Library Amanuel Lemma CS252 Algorithms.
Structuring Images CS1316: Representing Structure and Behavior.
Graph Terms By Susan Ott. Vertices Here are 7 vertices without any edges Each Vertex is labeled a different color and number.
Midwestern State University Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm 1.
Lecture 20. Graphs and network models 1. Recap Binary search tree is a special binary tree which is designed to make the search of elements or keys in.
Minimum Spanning Tree Chapter 13.6.
Great Theoretical Ideas in Computer Science
Minimum Spanning Trees and Shortest Paths
I206: Lecture 15: Graphs Marti Hearst Spring 2012.
Lists (and other data structures) that Loop
Minimum Spanning Tree Neil Tang 3/25/2010
Connected Components Minimum Spanning Tree
Minimum Spanning Tree.
Graphs Chapter 11 Objectives Upon completion you will be able to:
CSE 373 Data Structures and Algorithms
Spanning Trees.
Chapter 9: Graphs Basic Concepts
CS 583 Analysis of Algorithms
Minimum Spanning Tree Section 7.3: Examples {1,2,3,4}
Chapter 11 Graphs.
Minimum Spanning Tree Neil Tang 4/3/2008
Networks Kruskal’s Algorithm
Finally! Making the Villagers
CSE 373: Data Structures and Algorithms
Section 13.4 Trees.
Chapter 9: Graphs Basic Concepts
Problem Solving with Data Structures using Java: A Multimedia Approach
Presentation transcript:

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.jpggal1-right1.jpg

Walking (roughly, like Frankenstein) gal1-rightface.jpg gal1-right2.jpg gal1-right1.jpg gal1-rightface.jpg

As a Circular Linked List gal1-rightface.jpg gal1-right2.jpg gal1-right1.jpg gal1-rightface.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 movie

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 the Circular 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.jpggal1-right2.jpg gal1-right1.jpgJust change the constructor and the rest should work!

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 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 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 st.html st.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.”

MST Animation