Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 206 Introduction to Computer Science II 11 / 09 / 2009 Instructor: Michael Eckmann.

Similar presentations


Presentation on theme: "CS 206 Introduction to Computer Science II 11 / 09 / 2009 Instructor: Michael Eckmann."— Presentation transcript:

1 CS 206 Introduction to Computer Science II 11 / 09 / 2009 Instructor: Michael Eckmann

2 Michael Eckmann - Skidmore College - CS 206 - Fall 2009 Today’s Topics Questions? Comments? Heapsort Graphs

3 Heaps Someone remind us what a heap is.

4 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 at the deepest level all the nodes are as far left as possible if node is <= children, then it is called a minHeap

5 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 --- we went through this procedure before –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)‏ i = i-1 –do the above 3 steps until the size of the tree we are heapifying is one (i=0)‏ –The array is now sorted from low to high. Let's go through an example.

6 Heaps Recall --- to heapify an arbitrary binary tree (or an array)‏ Algorithm: –We find the last node that is NOT a leaf. Do a downward reheapification from that node. –Then the next to last node that is NOT a leaf and do a downward reheapification from that node. –Etc. –Until get to the root and do a downward reheapification from the root.

7 Heaps If we wanted to write heap sort in our current heap class, we could tweak our current downwardReheapify method in some way. How? Let's write heap sort iteratively and then recursively.

8 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.

9 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.

10 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

11 Graphs Edges often have a weight associated with them. An edge's weight is some numeric value. Can someone define a tree (assuming the nodes are vertices and the edges are undirected) in graph terms?

12 Graphs Edges often have a weight associated with them. An edge's weight is some numeric value. Can someone define a tree (assuming the nodes are vertices and the edges are undirected) in graph terms? –Trees are connected graphs without cycles.

13 Graphs Examples of using graphs to represent real world problems –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. –History of computer programming languages (directed edge from a language that influenced a later language)‏ Is it possible that this is a tree? why/why not?

14 Graphs Graphs can be represented in programs in many ways. Two common ways to represent them are: –adjacency matrix --- each row number r represents a vertex and the value at column c is true if there's an edge from vertex r to vertex c, false otherwise Notice that the type stored in the adjacency matrix is boolean Could we use this for weighted graphs? Could we use this for directed graphs? –edge lists store a linked list for each vertex, v i items in the list are those vertices v j for which there's an edge from v i to v j

15 Graphs Graph traversal –Breadth first search (BFS)‏ Pick a vertex at which to start Visit all of the adjacent vertices to the start vertex Then for each of the adjacent vertices, visit their adjacent vertices And so on until there are no more adjacent vertices Do not visit a vertex more than once –Only vertices that are reachable from the start vertex will be visited --- example on the board. –The order that vertices in a BFS are visited are in increasing order of length of path from starting vertex. –Those that have the same path length from the start vertex can be visited in any order. –Example of BFS on the board.

16 Graphs Implementation of breadth first search –Need a flag for each vertex to mark it as unvisited, waiting, or visited – so we don't visit vertices more than once. –Keep a queue which will hold the vertices to be visited –Keep a list of vertices as they are visited –BFS algorithm: Mark all vertices as unvisited Initially enqueue a vertex into the queue, mark it as waiting While the queue is not empty –Dequeue a vertex from the queue –Put it in the visited list, mark it as visited –Enqueue all the adjacent vertices that are marked as unvisited to the vertex just dequeued. –Mark the vertices just enqueued as waiting

17 Graphs Let's see how far we get with implementation of a graph and breadth first search.


Download ppt "CS 206 Introduction to Computer Science II 11 / 09 / 2009 Instructor: Michael Eckmann."

Similar presentations


Ads by Google