Download presentation
Presentation is loading. Please wait.
1
CS 206 Introduction to Computer Science II 10 / 31 / 2008 Happy Halloween!!! Instructor: Michael Eckmann
2
Michael Eckmann - Skidmore College - CS 206 - Fall 2008 Today’s Topics Questions? Heaps Heapsort Start discussion of Graphs
3
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.
4
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]
5
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
6
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.
7
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
8
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
9
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.
10
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.
11
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.
12
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.
13
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.
14
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.
15
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.
16
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
17
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.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.