CS 415, Section 001 | Sonoma State University | Fall, 2022 |
Algorithm Analysis
|
||
Instructor: Henry M. Walker
Lecturer, Sonoma State University |
Although much of this course is well developed, some details can be
expected to evolve as the semester progresses.
Any changes in course details will be announced promptly during class.
This worksheet/lab is in two parts: graphs and heaps.
This section in graphs contains exercises on
Consider the following adjacency matrix S for a weighted, directed graph. (Note: 0 means there is no edge.)
A | B | C | D | E | F | G | |
---|---|---|---|---|---|---|---|
A | 0 | 10 | 6 | 4 | 0 | 7 | 0 |
B | 10 | 0 | 9 | 0 | 11 | 0 | 18 |
C | 6 | 9 | 0 | 1 | 1 | 0 | 0 |
D | 4 | 0 | 1 | 0 | 1 | 2 | 0 |
E | 0 | 11 | 1 | 1 | 0 | 0 | 14 |
F | 7 | 0 | 0 | 2 | 0 | 0 | 15 |
G | 0 | 18 | 0 | 0 | 14 | 15 | 0 |
Draw a picture of the graph represented by this adjacency matrix S without the weights.
Can this graph be considered as a directed graph? As an undirected graph? Explain.
List the vertices in depth-first order beginning with vertex A. When you have a choice in processing vertices, pick them in alphabetical order; that is, when working with a stack and more than one vertex may be selected, push the vertices onto the stack in alphabetical order.
List the vertices in breadth-first order beginning with vertex A. Again, when you have a choice among vertices, pick them in alphabetical order; that is, when working with a queue and more than one vertex may be selected, enqueue the vertices onto the stack in alphabetical order.
Next, consider the graph with the following adjacency matrix.
A | B | C | D | E | F | G | |
A | 0 | 10 | 6 | 4 | 0 | 7 | 0 |
B | 0 | 0 | 9 | 0 | 11 | 0 | 18 |
C | 6 | 9 | 0 | 1 | 1 | 0 | 0 |
D | 4 | 0 | 1 | 0 | 1 | 2 | 0 |
E | 0 | 0 | 1 | 1 | 0 | 0 | 14 |
F | 7 | 0 | 0 | 2 | 0 | 0 | 15 |
G | 0 | 18 | 0 | 0 | 0 | 0 | 0 |
Draw a picture of the graph represented by this adjacency matrix S without the weights.
Is the graph directed or undirected? Explain.
List the vertices in depth-first order beginning with Vertex A. When there is a choice, process the vertices and edges from left to right.
List the vertices in breadth-first order beginning with Vertex A. Again, when there is a choice, process the vertices and edges from left to right.
Consider the following graph.
Write the adjacency matrix for this graph, based upon the alphabetical ordering of the vertices given.
Draw the adjacency list representation for this graph.
Write a careful analysis for the following.
Suppose a connected graph has v vertices and e edges. What is the complexity of a depth-first search?
Assume the queue is implemented with an array, and the graph by an adjacency matrix.
Assume the queue is implemented with a linked list, and the graph with adjacency lists.
A claim is made that a complete directed graph of n vertices has n(n-1) edges, while a complete undirected graph of n vertices has n(n-1)/2 edges. Is this claim true? If so outline a proof. If not, give a counter example.
Hint: You could use an argument related to permutations and combinations from Discrete Mathematics (e.g., CS 242).
This section is divided into
A max-heap is a binary tree with the property that the value at each node is larger (according to some ordering) than the values stored in either child. Similarly, a min-heap is a binary tree with the property that the value at each node is smaller (according to some ordering) than the values stored in either child.
Further, we restrict our attention to trees which are completely balanced. All nodes have 2 children, except at the bottom of the tree. The last row of the tree may not be full, but any items on the last level are moved left as much as possible. For example, the following tree illustrates the shape of a tree and the ordering of values within the tree for a max-heap.
To identify the nodes in a tree, the simplest approach is to number the nodes level-by-level from the root downward. Within a level, nodes are numbered left to right. Also, for simplicity, many textbooks assign the index 1 to the root node.
In examining the label nodes, a pattern emerges: for each node labeled i, the left child has label 2*i and the right node has label 2*i+1.
Note: As previously noted, some textbooks, such as Data Structures and Problem Solving Using Java, Fourth Edition by Mark Allen Weiss. uses this numbering scheme in writing code, with the top node numbered 1. If one considers an array element 0, Weiss suggests filling that position with -∞ for a min-heap or ∞ for a max-heap.
If we start labeling at 0 rather than 1, the labeling of nodes becomes:
In the context of 0-based labeling, identify a pattern for moving from a node with label j to its left child and its right child. What labels would be found on the left and right nodes of a node with label j?
Since nodes in a heap fit a straight forward identification system, there is a natural mapping between the logical nodes in a heap and the indexed elements of an array.
In class we considered how to insert an item into a heap and maintain the structure. In each case, we first place the item at the end of a tree (as far left as possible in the last row). The item then is worked up from its position, until the data in the tree are appropriately ordered.
The following heap repeats the structure given at the beginning of this lab:
Using this structure as a start, insert the values 30, 25, 55, 81, and 95. Show the structure of the tree and the values in each node after each insertion.
In class, we also considered how to remove the top-priority item from a heap: remove the root as the item to be returned, move the last item from the end of the heap to the root, and work that item down in the heap until the data are properly ordered.
In class, we discussed starting with an array of data and working from the bottom toward the top to rearrange the data to yield a heap. For Steps 4 and 5, consider working with a min-heap.
In the following questions, consider a heap implemented with 0-based indexing.
Suppose an array of twelve elements, a[12], is initialized with a[i] = 20-i for each i. What rearrangements, if any, need to be done in order to make the corresponding tree structure into a heap? Show the data in the array once a heap is achieved.
Suppose the array of twelve elements, a[12], is initialized with a[i] = i for each i. What rearrangements, if any, need to be done in order to make the corresponding tree structure into a heap? Show the data in the array once a heap is achieved.
created December 1, 2018 revised December 2, 2018 revised December 27-30, 2021 revised February 4, 2022 reformatted and heap material added July 28, 2022 |
|
For more information, please contact Henry M. Walker at walker@cs.grinnell.edu. |
Copyright © 2011-2022
by Henry M. Walker.
|