Abstract Data Types and Subprograms

Slides:



Advertisements
Similar presentations
Abstract Data Types and Algorithms
Advertisements

Binary Trees Chapter 6. Linked Lists Suck By now you realize that the title to this slide is true… By now you realize that the title to this slide is.
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.
Abstract Data Types and Subprograms
Chapter 8 Abstract Data Types and Subprograms. 2 Chapter Goals Distinguish between an array-based visualization and a linked visualization Distinguish.
Graphs CS3240, L. grewe.
Advanced Data Structures
Fall 2007CS 2251 Trees Chapter 8. Fall 2007CS 2252 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information.
Toy Problem: Missionaries and Cannibals
Trees Chapter 8. Chapter 8: Trees2 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information To learn how.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.
Graphs. Graph definitions There are two kinds of graphs: directed graphs (sometimes called digraphs) and undirected graphs Birmingham Rugby London Cambridge.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (4) Data Structures 11/18/2008 Yang Song.
©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 03 / 25 / 2009 Instructor: Michael Eckmann.
Transforming Infix to Postfix
CS 206 Introduction to Computer Science II 11 / 09 / 2009 Instructor: Michael Eckmann.
Graphs Chapter 28 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
More Trees COL 106 Amit Kumar and Shweta Agrawal Most slides courtesy : Douglas Wilhelm Harder, MMath, UWaterloo
12 Abstract Data Types Foundations of Computer Science ã Cengage Learning.
Trees & Graphs Nell Dale & John Lewis (adaptation by Michael Goldwasser and Erin Chambers)
Chapter 9 Abstract Data Types and Algorithms. 2 Abstract Data Types Abstract data type A data type whose properties (data and operations) are specified.
Advanced Algorithms Analysis and Design Lecture 8 (Continue Lecture 7…..) Elementry Data Structures By Engr Huma Ayub Vine.
Tree Searching Breadth First Search Dept First Search.
Chapter 9 Abstract Data Types and Algorithms. 2 Chapter Goals Define an abstract data type and discuss its role in algorithm development Distinguish between.
Chapters 7, 8, & 9 Quiz 3 Review 1. 2 Algorithms Algorithm A set of unambiguous instructions for solving a problem or subproblem in a finite amount of.
Chapter 14 Graphs. © 2004 Pearson Addison-Wesley. All rights reserved Terminology G = {V, E} A graph G consists of two sets –A set V of vertices,
Spring 2010CS 2251 Trees Chapter 6. Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information.
1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),
Chapter 9 Priority Queues, Heaps, Graphs, and Sets.
Chapter 9 (modified) Abstract Data Types and Algorithms Nell Dale John Lewis.
Chapter 9 Abstract Data Types and Algorithms Nell Dale John Lewis.
9-1 Abstract Data Types Abstract data type A data type whose properties (data and operations) are specified independently of any particular implementation.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Graphs. Made up of vertices and arcs Digraph (directed graph) –All arcs have arrows that give direction –You can only traverse the graph in the direction.
Chapter 9 Priority Queues, Heaps, Graphs 1 Fall 2010.
Data Structures Chapter 6. Data Structure A data structure is a representation of data and the operations allowed on that data. Examples: 1.Array 2.Record.
Chapter 12 Abstract Data Type. Understand the concept of an abstract data type (ADT). Understand the concept of a linear list as well as its operations.
Chapter 12 Abstract Data Type. Understand the concept of an abstract data type (ADT). Understand the concept of a linear list as well as its operations.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 13: Graphs Data Abstraction & Problem Solving with C++
Chapter 8 Abstract Data Types and Subprograms. 2 Chapter Goals Distinguish between an array-based visualization and a linked visualization Distinguish.
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.
COSC 2007 Data Structures II
© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.
CMSC 202, Version 5/02 1 Trees. CMSC 202, Version 5/02 2 Tree Basics 1.A tree is a set of nodes. 2.A tree may be empty (i.e., contain no nodes). 3.If.
Chapter 20: Graphs. Objectives In this chapter, you will: – Learn about graphs – Become familiar with the basic terminology of graph theory – Discover.
Chapter 9 Abstract Data Types and Algorithms Nell Dale John Lewis.
Data Structures and Algorithms Lists, Stacks, Queues, and Graphs Sorting and searching algorithms.
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.
DATA STRUCURES II CSC QUIZ 1. What is Data Structure ? 2. Mention the classifications of data structure giving example of each. 3. Briefly explain.
CPS120: Introduction to Computer Science Nell Dale John Lewis Abstract Data Types.
CPS120: Introduction to Computer Science Sorting.
Section 9.6 Graph Applications. 9.6 Graph Applications Our graph specification does not include traversal operations. We treat traversal as a graph application/algorithm.
Chapter 12 Abstract Data Type.
Graphs Chapter 20.
Trees.
Abstract Data Types and Subprograms
Introduction to Data Structure
Chapter 20: Binary Trees.
Chapter 21: Binary Trees.
Graphs Chapter 13.
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Graphs.
CSC 143 Java Trees.
Important Problem Types and Fundamental Data Structures
Trees.
Presentation transcript:

Abstract Data Types and Subprograms Chapter 8 Abstract Data Types and Subprograms

What is Computer Science? One More Definition of Computer Science: “Computer Science is the Automation of Abstractions” – anonymous

Data Instructions More Simply Stated… Data organization and Algorithm affect each other

Chapter Goals array-based implementation linked implementation What is an Abstract Data Type? Concept of “The Separation of Interface from Implementation” array-based implementation linked implementation

More Chapter Goals Some Specific Common Abstract Data Types arrays and lists stacks and queues binary trees and binary search trees Graphs Common Algorithms that Operate on these ADT’s Tree Searches Traveling Salesman Problem, etc

Abstract Data Types Abstract data type A composite data type containing: Data in a particular organization Operations (algorithms) to operate on that data Remember the most powerful tool for managing complexity?

Abstract Data Types Abstract data type The goals are to: Reduce complexity thru abstraction Organize our data into various kinds of containers Think about our problem in terms of data and the operations (algorithms) that are done to them

Why do We Need ADT’s? Very DIFFICULT to write this without ADT’s

Why do We Need ADT’s? Almost IMPOSSIBLE to write this without ADT’s

All operations occur at the top Stacks All operations occur at the top

structures that are stacks An abstract data type in which accesses are made at only one end LIFO, which stands for Last In First Out The insert is called Push and the delete is called Pop Name some everyday structures that are stacks

Hand simulate this algorithm Stacks WHILE (more data) Read value Push(myStack, value) WHILE (NOT IsEmpty(myStack)) Pop(myStack, value) Write value Hand simulate this algorithm 12 12

All operations occur at the front and back Queues All operations occur at the front and back

Queues Queue An abstract data type in which items are entered at one end and removed from the other end FIFO, for “First In First Out” EnQue: Get in line at rear Deque: Get served at front

Hand simulate this algorithm Queues WHILE (more data) Read value Enque(myQueue, value) WHILE (NOT IsEmpty(myQueue)) Deque(myQueue, value) Write value Hand simulate this algorithm 15

Stacks and Queues (using a Linked Implementation) Stack and queue visualized as linked structures

Implementation: What’s Inside? There are several ways to implement any ADT 2 Common implementations use: An Array Linked Nodes Here, we are concerned with the details inside the ADT (Building the car instead of Driving the car)

ADT Implementations Array-based implementation Items are in an array, physically next to each other in memory Linked-based implementation Items are not next to each other in memory, instead each item points to the next item Did you ever play treasure hunt, a game in which each clue told you where to go to get the next clue?

Array-Based Implementations

Linked Implementations

Algorithm for Playing Solitaire WHILE (deck not empty) Pop the deckStack Check for Aces While (There are playStacks to check) If(can place card) Push card onto playStack Else push card onto usedStack Does implementation matter at this point?

“Logical Level” The algorithm that uses the list does not need to know how it is implemented We have written algorithms using a stack, a queue, and a list without ever knowing the internal workings of the operations on these containers 22 22

Can represent more complex relationships between data Trees Can represent more complex relationships between data

Trees What is the unique path to the node containing 5? 9? 7? … Root node Node with two children Node with right child Leaf node What is the unique path to the node containing 5? 9? 7? … Node with left child

Why Trees? Some real-world data is tree-like Geneology Family trees Management Hierarchies File Systems (Folders etc) Treeses are easy to search

Binary Search Trees Binary search tree Each “sub-tree” has the following property(s): All sub-trees on one side are greater All sub-tress on the other side are smaller

Binary Search Tree Each node is the root of a subtree made up of its left and right children Prove that this tree is a BST Figure 8.7 A binary search tree

Binary Search Tree (A Look at Implementation Details)

Like Mona Lisa, Trees have repeating patterns at smaller levels Trees and Recursion Like Mona Lisa, Trees have repeating patterns at smaller levels

Recursive Binary Search Algorithm Boolean BinSearch(node, item) If (node is null) item does not exist Else If (item < node) BinSearch(node.leftchild, item) BinSearch(node.rightchild, item)

Binary Search Tree

Another Binary Search Tree 32 32

Building Binary Search Tree Insert(tree, item) IF (tree is null) Put item in tree ELSE IF (item < info(tree)) Insert (left(tree), item) Insert (right(tree), item) 33 33

Can represent more complex relationships between data Graphs Can represent more complex relationships between data

Graphs Graph A set of nodes and a set of edges that relate the nodes to each other Undirected graph Edges have no direction Directed graph (Digraph) Each edge has a direction (arrowhead) Weighted Graph Edges have values

Graphs Figure 8.10 Examples of graphs

Graphs Figure 8.10 Examples of graphs

Graphs Figure 8.10 Examples of graphs

Common Graph Algorithms Traveling salesman problem Finding the cheapest or shortest path through several cities Internet data routing algorithms Family tree software Neural Nets (An Artificial Intelligence technique)

Graph Algorithms A Depth-First Searching Algorithm--Given a starting vertex and an ending vertex, we can develop an algorithm that finds a path from startVertex to endVertex This is called a depth-first search because we start at a given vertex and go to the deepest branch and exploring as far down one path before taking alternative choices at earlier branches

Depth First Search(startVertex, endVertex) Set found to FALSE Push(myStack, startVertex) WHILE (NOT IsEmpty(myStack) AND NOT found) Pop(myStack, tempVertex) IF (tempVertex equals endVertex) Write endVertex Set found to TRUE ELSE IF (tempVertex not visited) Write tempVertex Push all unvisited vertexes adjacent with tempVertex Mark tempVertex as visited IF (found) Write "Path has been printed" ELSE Write "Path does not exist")

Can we get from Austin to Washington? Figure 8.11 Using a stack to store the routes

Can we get from Austin to Washington? Figure 8.12, The depth-first search

Breadth-First Search What if we want to answer the question of how to get from City X to City Y with the fewest number of airline stops? A Breadth-First Search answers this question A Breadth-First Search examines all of the vertices adjacent with startVertex before looking at those adjacent with those adjacent to these vertices A Breadth-First Search uses a queue, not a stack, to answer this above question Why??

Breadth First Search(startVertex, endVertex) Set found to FALSE Enque(myQueue, startVertex) WHILE (NOT IsEmpty(myQueue) AND NOT found) Deque(myQueue, tempVertex) IF (tempVertex equals endVertex) Write endVertex Set found to TRUE ELSE IF (tempVertex not visited) Write tempVertex Enque all unvisited vertexes adjacent with tempVertex Mark tempVertex as visited IF (found) Write "Path has been printed" ELSE Write "Path does not exist"

How can I get from Austin to Washington in the fewest number of stops? Figure 8.13 Using a queue to store the routes

Breadth-First Search Traveling from Austin to Washington, DC Figure 8.14, The Breadth-First Search