Problem Solving with Data Structures using Java: A Multimedia Approach Chapter 12: Circular Linked Lists And Graphs: Lists and Trees That Loop.

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
Graphs Chapter 30 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X.
Graphs Graphs are the most general data structures we will study in this course. A graph is a more general version of connected nodes than the tree. Both.
Graphs By JJ Shepherd. Introduction Graphs are simply trees with looser restrictions – You can have cycles Historically hard to deal with in computers.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 21: Graphs.
Graph Algorithms: Minimum Spanning Tree We are given a weighted, undirected graph G = (V, E), with weight function w:
Spanning Trees.
Graphs.
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
Graphs Chapter 28 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
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.
Problem Solving with Data Structures using Java: A Multimedia Approach Chapter 8: Trees of Images.
Math for Liberal Studies.  Here is a map of the parking meters in a small neighborhood  Our goal is to start at an intersection, check the meters, and.
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.
Lists (and other data structures) that Loop CS1316: Representing Structure and Behavior.
CS 146: Data Structures and Algorithms July 21 Class Meeting
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
Computer Science 112 Fundamentals of Programming II Introduction to Graphs.
Graph Dr. Bernard Chen Ph.D. University of Central Arkansas.
Nattee Niparnan. Graph  A pair G = (V,E)  V = set of vertices (node)  E = set of edges (pairs of vertices)  V = (1,2,3,4,5,6,7)  E = ((1,2),(2,3),(3,5),(1,4),(4,
Fundamentals, Terminology, Traversal, Algorithms Graph Algorithms Telerik Algo Academy
Representing and Using Graphs
Dijkstra’s Algorithm. Announcements Assignment #2 Due Tonight Exams Graded Assignment #3 Posted.
Can you connect the dots as shown without taking your pen off the page or drawing the same line twice.
Graphs. 2 Graph definitions There are two kinds of graphs: directed graphs (sometimes called digraphs) and undirected graphs Birmingham Rugby London Cambridge.
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.
10 Copyright © William C. Cheng Data Structures - CSCI 102 Graph Terminology A graph consists of a set of Vertices and a set of Edges C A B D a c b d e.
Agenda Review: –Planar Graphs Lecture Content:  Concepts of Trees  Spanning Trees  Binary Trees Exercise.
QUEUES What are Queues? Creating a Queue Enqueuing and Dequeuing Testing for an Empty Queue.
CSE 373: Data Structures and Algorithms
Trees, Binary Search Trees, Balanced Trees, Graphs Graph Fundamentals Telerik Algo Academy
1 Directed Graphs Chapter 8. 2 Objectives You will be able to: Say what a directed graph is. Describe two ways to represent a directed graph: Adjacency.
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.
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.
Teacher Notes: There is a famous problem in Discrete Mathematics called ‘The Bridges of Konigsberg’ in which it is said to be impossible to cross each.
Graphs. What is a graph? In simple words, A graph is a set of vertices and edges which connect them. A node (or vertex) is a discrete position in the.
Nattee Niparnan. Graph  A pair G = (V,E)  V = set of vertices (node)  E = set of edges (pairs of vertices)  V = (1,2,3,4,5,6,7)  E = ((1,2),(2,3),(3,5),(1,4),(4,
Problem Solving with Data Structures using Java: A Multimedia Approach Chapter 9: Lists and Trees for Structuring Sounds.
Graphs Chapter 15 introduces graphs which are probably the most general and commonly-used data structure. This lecture introduces heaps, which are used.
Discrete Maths 9. Graphs Objective
Graph.
Lists (and other data structures) that Loop
Graphs Chapter 11 Objectives Upon completion you will be able to:
CSE 373 Data Structures and Algorithms
Spanning Trees.
Chapter 9: Graphs Basic Concepts
Graphs.
Minimum Spanning Tree Section 7.3: Examples {1,2,3,4}
Chapter 11 Graphs.
Graphs.
Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013
Networks Kruskal’s Algorithm
CSE 373: Data Structures and Algorithms
Algorithms: Design and Analysis
Graphs.
Graphs.
CSE 373: Data Structures and Algorithms
Graphs.
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Graphs.
Chapter 9: Graphs Basic Concepts
Problem Solving with Data Structures using Java: A Multimedia Approach
For Friday Read chapter 9, sections 2-3 No homework
Presentation transcript:

Problem Solving with Data Structures using Java: A Multimedia Approach Chapter 12: Circular Linked Lists And Graphs: Lists and Trees That Loop

Chapter Learning Goals

Story Cell animation An example of when it’s useful to have a list reference itself. Graphs: Trees that reference themselves

Mario didn’t *really* move

Characters from MediaSources

Walking (roughly, like Frankenstein)

As a Circular Linked List

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 WalkingKid

WalkingKid movie

WalkingKid Class

WalkingKid circular list

Walking

Walking (part 2)

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? Just 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()); }

How could we make this work without errors? Lots of ways! Here’s two: Have a visited flag. Mark it true when visited. Don’t visit already visited. Need a reset() method. Have two next links. One for rendering. The other for walking all nodes for traversing for add() or remove().

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.

A Map as a Graph

Adding weights

Getting a minimal spanning tree

Question: Find the minimal spanning tree

Answer