CS 206 Introduction to Computer Science II 10 / 31 / 2008 Happy Halloween!!! Instructor: Michael Eckmann.

Slides:



Advertisements
Similar presentations
CS 206 Introduction to Computer Science II 04 / 01 / 2009 Instructor: Michael Eckmann.
Advertisements

CS 206 Introduction to Computer Science II 03 / 23 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 22 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 07 / 2008 Instructor: Michael Eckmann.
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
1 HeapSort CS 3358 Data Structures. 2 Heapsort: Basic Idea Problem: Arrange an array of items into sorted order. 1) Transform the array of items into.
CS 206 Introduction to Computer Science II 11 / 11 / Veterans Day Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 12 / 05 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 04 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 20 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 10 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 03 / 20 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 09 / 22 / 2008 Instructor: Michael Eckmann.
Chapter 10 Heaps Anshuman Razdan Div of Computing Studies
CS 206 Introduction to Computer Science II 11 / 12 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 03 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 12 / 03 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 29 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 03 / 16 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 03 / 25 / 2009 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 08 / 2010 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 12 / 10 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 09 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 02 / 11 / 2009 Instructor: Michael Eckmann.
CST 230 Razdan et alhttp://dcst2.east.asu.edu/~razdan/cst230/ Razdan with contribution from others 1 Tree Traversals Pre-order traversal –process root.
CS 206 Introduction to Computer Science II 03 / 30 / 2009 Instructor: Michael Eckmann.
Heapsort CIS 606 Spring Overview Heapsort – O(n lg n) worst case—like merge sort. – Sorts in place—like insertion sort. – Combines the best of both.
1 Foundations of Software Design Fall 2002 Marti Hearst Lecture 17: Binary Search Trees; Heaps.
CS 206 Introduction to Computer Science II 10 / 28 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 09 / 30 / 2009 Instructor: Michael Eckmann.
Heapsort Based off slides by: David Matuszek
1 HEAPS & PRIORITY QUEUES Array and Tree implementations.
Compiled by: Dr. Mohammad Alhawarat BST, Priority Queue, Heaps - Heapsort CHAPTER 07.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
ADT Table and Heap Ellen Walker CPSC 201 Data Structures Hiram College.
Heapsort CSC Why study Heapsort? It is a well-known, traditional sorting algorithm you will be expected to know Heapsort is always O(n log n)
Introduction to Graphs. Introduction Graphs are a generalization of trees –Nodes or verticies –Edges or arcs Two kinds of graphs –Directed –Undirected.
The Binary Heap. Binary Heap Looks similar to a binary search tree BUT all the values stored in the subtree rooted at a node are greater than or equal.
Chapter 21 Binary Heap.
P p Chapter 10 has several programming projects, including a project that uses heaps. p p This presentation shows you what a heap is, and demonstrates.
CS 206 Introduction to Computer Science II 10 / 05 / 2009 Instructor: Michael Eckmann.
Priority Queue. Priority Queues Queue (FIFO). Priority queue. Deletion from a priority queue is determined by the element priority. Two kinds of priority.
PRIORITY QUEUES AND HEAPS CS16: Introduction to Data Structures & Algorithms Tuesday, February 24,
CS 206 Introduction to Computer Science II 11 / 16 / 2009 Instructor: Michael Eckmann.
COSC 2007 Data Structures II Chapter 14 Graphs I.
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.
Queues, Stacks and Heaps. Queue List structure using the FIFO process Nodes are removed form the front and added to the back ABDC FrontBack.
CS 206 Introduction to Computer Science II 10 / 02 / 2009 Instructor: Michael Eckmann.
Heaps and Heapsort Prof. Sin-Min Lee Department of Computer Science San Jose State University.
Heapsort. What is a “heap”? Definitions of heap: 1.A large area of memory from which the programmer can allocate blocks as needed, and deallocate them.
CSC212 Data Structure Lecture 16 Heaps and Priority Queues Instructor: George Wolberg Department of Computer Science City College of New York.
Lecture 15 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
Heaps & Priority Queues
Lecture 8 : Priority Queue Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University.
AVL Trees and Heaps. AVL Trees So far balancing the tree was done globally Basically every node was involved in the balance operation Tree balancing can.
Graphs Upon completion you will be able to:
1Computer Sciences. 2 HEAP SORT TUTORIAL 4 Objective O(n lg n) worst case like merge sort. Sorts in place like insertion sort. A heap can be stored as.
1 Chapter 6 Heapsort. 2 About this lecture Introduce Heap – Shape Property and Heap Property – Heap Operations Heapsort: Use Heap to Sort Fixing heap.
Chapter 05 Introduction to Graph And Search Algorithms.
Lecture on Data Structures(Trees). Prepared by, Jesmin Akhter, Lecturer, IIT,JU 2 Properties of Heaps ◈ Heaps are binary trees that are ordered.
"Teachers open the door, but you must enter by yourself. "
Priority Queues.
i206: Lecture 14: Heaps, Graphs intro.
Graphs Chapter 11 Objectives Upon completion you will be able to:
Priority Queues.
"Teachers open the door, but you must enter by yourself. "
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Heaps Chapter 6 Section 6.9.
Presentation transcript:

CS 206 Introduction to Computer Science II 10 / 31 / 2008 Happy Halloween!!! Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS Fall 2008 Today’s Topics Questions? Heaps Heapsort Start discussion of Graphs

Heaps A heap is a binary tree in which the elements can be compared with a total order semantics AND the arrangement of the values in the heap follow the following rules. –1) the element in each node is >= the elements of that node's children (maxHeap)‏ –2) the tree is complete - every level except the deepest must contain as many nodes as possible and the deepest level all the nodes are as far left as possible if node is <= children, then it is called a minHeap Let's look at some examples of heaps and non-heaps on the board.

Heaps Recall the array implementation of a binary tree. The array implementation works well for complete binary trees. Node [i] has it's children (if they exist) at left child: [2i+1] right child: [2i+2] If Node [i] is not the root, then Node [i]'s parent is at [(i-1)/2]

Heaps To insert a new node into a heap –1) after the node is inserted the structure still must be a heap that is it is a complete binary tree with the element in each node >= the elements of that node's children

Heaps To insert a new node into a heap –Place the node in the next available slot (deepest level and to the “right” of the last node in that level)‏ –Then, check that node against it's parent and swap if appropriate. Continue to do this until we don't need to swap. We swap when a node's parent is < the node. –When this process is done, we still have a heap. The process of rising a node into it's proper place is (upward) heapification. To heapify is to start with a complete binary tree that is not a heap and make it a heap.

Heaps To delete the root from the heap –1) after the root is removed from the heap, the structure still must be a heap that is it must be a complete binary tree with the element in each node >= the elements of that node's children

Heaps To delete the root from the heap –if the tree consists of only one node, the result is an empty tree –if the tree consists of more than one node, then move the last element (the one furthest to the right in the deepest level) in the tree to the root now we have a complete binary tree with one less node but it may not be a heap, so we need to heapify, but this time we need to do a (downward) heapification. call the element that moved to the root, the out-of- place node while (out-of-place node is < one of its children)‏ –swap the out-of-place node with its larger child

Heaps Heaps can be used to implement Priority Queues –Main operations of a priority queue remove (highest priority item)‏ add For a Heap implementation of a priority queue, we would remove from the root (and then make sure the heap remains a heap.)‏ For add, we would place at last slot and heapify.

Heaps To heapify an arbitrary binary tree (or an array)‏ –We can heapify parts of the tree and eventually end up with the whole binary tree heapified. –Example in the handout.

Heaps Another way to heapify data is to add an item into the tree one at a time and reheapify at each step. Eg. Given 9, 12, 17, 30, 50, 20, 60, 65, 4, 19 Add in 9 to the tree. It is the only node so it is a heap already. Add in 12 as the last element in the tree and do upward reheapification to swap 9 and 12. Add in 17 as the last element in the tree and do upward reheapification... and so on until we added in all nodes and reheapified at each step. You end up with a heap.

Heaps There is a sorting algorithm based on heaps called HeapSort. The algorithm is simply: –start with n unsorted data items –create a maxHeap (of size n) out of these items --- store it as an array –set i = n -1 swap the root (index 0) and last node (index i)‏ reheapify (downward reheapification) the tree that starts at the root (index 0) and goes to i-1 (do not include the nodes at i and higher in the new heap)‏ –recursively do this until the size of the tree we are heapifying is one (i=0)‏ –The array is now sorted from low to high. Draw on the board.

Heaps Let's implement a heap as an array. Let's implement code for a heap and methods to do downward and upward reheapification. Then we'll implement the heapSort algorithm.

Graphs Graphs consist of a set of vertices and a set of edges. An edge connects two vertices. Edges can be directed or undirected. Directed graphs' edges are all directed. Undirected graphs' edges are all undirected. Directed graphs are sometimes called digraphs. Two vertices are adjacent if an edge connects them. The degree of a vertex is the number of edges starting at the vertex. Two vertices v1 and v2 are on a path if there are a list of vertices starting at v1 and ending at v2 where each consecutive pair of vertices is adjacent.

Graphs The length of a path is the number of edges in the path. A simple path is one whose edges are all unique. A cycle is a simple path, starting and ending at the same vertex. A vertex is reachable from another vertex if there is a path between them. A graph is connected if all pairs of vertices in the graph have a path between them. Example on the board of a connected and an unconnected graph. A complete graph (aka fully connected graph) is a connected graph where all pairs of vertices in the graph are adjacent. Example on the board.

Graphs Connectivity of a digraph –A digraph is strongly connected if there is a path from any vertex to any other vertex (following the directions of the edges)‏ –A digraph is weakly connected if in its underlying undirected graph, there is a path from any vertex to any other vertex Degree of a vertex in a digraph –In-degree of a vertex is the number of edges entering the vertex –Out-degree of a vertex is the number of edges leaving the vertex

Graphs Edges often have a weight associated with them. An edge's weight is some numeric value. Example on the board of using graphs to represent real world problems –e.g. A weighted graph that connect cities (vertices) and the weights of the edges might be length of time to travel between the two cities. –A graph whose vertices represent buildings on campus and edges are sidewalks connecting the buildings.