Presentation on theme: "Abstract Data Types and Subprograms"— Presentation transcript:
1Abstract Data Types and Subprograms Chapter 8Abstract Data Types and Subprograms
2What is Computer Science? One More Definition of Computer Science:“Computer Science is the Automation of Abstractions” – anonymous
3Data Instructions More Simply Stated… Data organization and Algorithm affect each other
4Chapter Goals array-based implementation linked implementation What is an Abstract Data Type?Concept of“The Separation of Interface from Implementation”array-based implementationlinked implementation
5More Chapter Goals Some Specific Common Abstract Data Types arrays and listsstacks and queuesbinary trees and binary search treesGraphsCommon Algorithms that Operate on these ADT’sTree SearchesTraveling Salesman Problem, etc
6Abstract Data Types Abstract data type A composite data type containing:Data in a particular organizationOperations (algorithms) to operate on that dataRemember the most powerful tool for managing complexity?
7Abstract Data Types Abstract data type The goals are to: Reduce complexity thru abstractionOrganize our data into various kinds of containersThink about our problem in terms of data and the operations (algorithms) that are done to them
8Why do We Need ADT’s?Very DIFFICULT to write this without ADT’s
9Why do We Need ADT’s?Almost IMPOSSIBLE to write this without ADT’s
10All operations occur at the top StacksAll operations occur at the top
11structures that are stacks An abstract data type in which accesses are made at only one endLIFO, which stands for Last In First OutThe insert is called Push and the delete is called PopName some everydaystructures that are stacks
12Hand simulate this algorithm StacksWHILE (more data)Read valuePush(myStack, value)WHILE (NOT IsEmpty(myStack))Pop(myStack, value)Write valueHand simulate this algorithm1212
13All operations occur at the front and back QueuesAll operations occur at the front and back
14QueuesQueueAn abstract data type in which items are entered at one end and removed from the other endFIFO, for “First In First Out”EnQue: Get in line at rearDeque: Get served at front
15Hand simulate this algorithm QueuesWHILE (more data)Read valueEnque(myQueue, value)WHILE (NOT IsEmpty(myQueue))Deque(myQueue, value)Write valueHand simulate this algorithm15
16Stacks and Queues (using a Linked Implementation) Stack and queue visualized as linked structures
17Implementation: What’s Inside? There are several ways to implement any ADT2 Common implementations use:An ArrayLinked NodesHere, we are concerned with the details inside the ADT(Building the car instead of Driving the car)
18ADT ImplementationsArray-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 itemDid you ever play treasure hunt, a game in which each cluetold you where to go to get the next clue?
21Algorithm for Playing Solitaire WHILE (deck not empty)Pop the deckStackCheck for AcesWhile (There are playStacks to check)If(can place card)Push card onto playStackElsepush card onto usedStackDoes implementation matter at this point?
22“Logical Level”The algorithm that uses the list does not need to know how it is implementedWe have written algorithms using a stack, a queue, and a list without ever knowing the internal workings of the operations on these containers2222
23Can represent more complex relationships between data TreesCan represent more complex relationships between data
24Trees What is the unique path to the node containing 5? 9? 7? … Root nodeNode with two childrenNode with right childLeaf nodeWhat is the unique pathto the node containing5? 9? 7? …Node with left child
25Why Trees? Some real-world data is tree-like Geneology Family treesManagement HierarchiesFile Systems (Folders etc)Treeses are easy to search
26Binary Search Trees Binary search tree Each “sub-tree” has the following property(s):All sub-trees on one side are greaterAll sub-tress on the other side are smaller
27Binary Search Tree Each node is the root of a subtree made up of its left andright childrenProve that thistree is a BSTFigure 8.7 A binary search tree
28Binary Search Tree (A Look at Implementation Details)
29Like Mona Lisa, Trees have repeating patterns at smaller levels Trees and RecursionLike Mona Lisa,Trees have repeating patterns at smaller levels
30Recursive Binary Search Algorithm Boolean BinSearch(node, item)If (node is null)item does not existElseIf (item < node)BinSearch(node.leftchild, item)BinSearch(node.rightchild, item)
33Building Binary Search Tree Insert(tree, item)IF (tree is null)Put item in treeELSEIF (item < info(tree))Insert (left(tree), item)Insert (right(tree), item)3333
34Can represent more complex relationships between data GraphsCan represent more complex relationships between data
35GraphsGraph 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
39Common Graph Algorithms Traveling salesman problemFinding the cheapest or shortest path through several citiesInternet data routing algorithmsFamily tree softwareNeural Nets (An Artificial Intelligence technique)
40Graph AlgorithmsA 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
41Depth 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")
42Can we get from Austin to Washington? Figure 8.11 Using a stack to store the routes
43Can we get from Austin to Washington? Figure 8.12, The depth-first search
44Breadth-First SearchWhat 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??
45Breadth First Search(startVertex, endVertex) Set found to FALSEEnque(myQueue, startVertex)WHILE (NOT IsEmpty(myQueue) AND NOT found)Deque(myQueue, tempVertex)IF (tempVertex equals endVertex)Write endVertexSet found to TRUEELSE IF (tempVertex not visited)Write tempVertexEnque all unvisited vertexes adjacent with tempVertexMark tempVertex as visitedIF (found)Write "Path has been printed"ELSEWrite "Path does not exist"
46How can I get from Austin to Washington in the fewest number of stops? Figure 8.13 Using a queue to store the routes
47Breadth-First Search Traveling from Austin to Washington, DC Figure 8.14, The Breadth-First Search