Data Structures and Algorithms Notes
Data Structures and Algorithms Notes
Section A
1)
a) Describe the relationship between Big "O" notation and analysis of algorithms
b) Discuss the procedure of determining the complexity of algorithms using appropriate examples
c) Given the algorithm below, give the expression for its running time (Show working)
Algorithm Power (Real x, Integer Result)
result x
for i 1 to n-1 do
result Result.x
return result
d) Reorder the following complexity from smallest to largest:
2n, n!, n10 ,24, nlog2(n). Justify your answer.
e) Calculate the big-O notation of and 3n4+nlog2 (n)
2) Consider the array data [26, 38, 12, 39, 27, 16]. For each of the following sorting algorithms, show a
trace of the swaps or data assignments executed during a run of the algorithm.
a) Selection Sort
b) Insertion Sort
c) Merge Sort
d) Quicksort in which the pivot is always selected from the left-hand side of the array.
Section B
1) Write a function to count the number of items in a linked list
a) Iteratively
b) Recursively
Write the corresponding C++ program to test the functions above.
2)
a) Imagine an unknown implementation of an ADT sorted list of integers. This ADT organizes its items
into ascending order. Suppose that you have just read N integers into one-dimensional array of
integers called Data. Write C++ statements that use the ADT list operations to sort the array into
ascending order
b) Write a pseudo code function Swap (L, I, J) that interchanges the items currently in position I and J of
a list. Define the function in terms of the ADT list operations, so that it is independent of any
particular implementation of the list. Assume that the list, in fact, has items at positions I and J. What
impact does this assumption have on your solutions?
3)
a) Write a function that will delete from a linked list of integers the node that contains the largest integer.
Can you do this with a traversal of the list?
b) Write a function to merge two linked lists of integers that are sorted into ascending order. The results
should be a third linked list that is the sorted combination of the original lists. Do not destroy the
original list.
4) We wish to store a list in an array whose cells consist of two fields; data to store an element and position
to give the (integer) position of the element. An integer last indicates that A [1] through A[last] are used to
hold the list. The type LIST can be defined by type
LIST = record
Last: integers;
Elements: arrange [1…max_length] of record
Data: element type
Position: integer
End;
a) Write a procedure DELETE (P, L) to remove the element at position P. Include all necessary error
checks.
b) Convert the algorithm in (a) into a C++ program and test it
5)
a) Write an implementation of the ADT list that uses a dynamically allocated array to represent the list
items
b) Let P be a pointer to the first node in a singly linked list. Write a procedure to delete every other node
beginning with node P (i.e. the first, third, fifth, etc nodes of the list are deleted)
8) Write a pointer-based implementation of the ADT two-ended list, which has insertion and deletion
operations at both ends of the list
a) Without a tail pointer
b) With a tail pointer
9) Write a function called MoveToFront that finds and moves a specified item to the front of the list. Write
this function for and array implemented list and a linked list. The function will first find the item and then
move the item to the front of the list. Be sure to use good programming style.
10) Given a weighted, undirected graph below.
a) Apply Dijkstra’s shortest path algorithm to find the shortest paths from node A to all the other nodes.
List the shortest paths and their total costs in each step, as well as the node selected in each step. Show
all the steps for full credit. No Java/C++ code required.
b) Provide the adjacency matrix of the graph shown above. Please note that this graph is weighted.
c) Construct a Minimum Spanning Tree (MST) of the graph shown above starting from node A.
Highlight the path chosen in each step in the following figures. No Java/C++ code required.
d) Please list each event (add or remove node #) and the correspondent content in the queue when
applying a Breadth-First Search on this graph starting from vertex 0. Each event involves adding one
node to or removing one node from the queue. If there is a choice of nodes to visit, choose the one
with the smallest label first.
11) Write a method to reverse all the data in a stack using a queue. An example is shown below.
The following methods in the stack and queue class could be used. Note you can only use one stack and
one queue in your method.
public class Stack{
void push(int);
int pop();
int peek();
boolean isEmpty();
int size();
}
public class Queue{
void enqueue(int);
int dequeue();
int peek();
boolean isEmpty();
int size();
}
12) A maximum heap is a heap that every node’s key is larger than the keys of its children. Please answer the
following questions. No java code required.
a) Insert the following sequence of numbers into a maximum heap, 9, 7, 15, 5, 10, 3. Show the steps
required to create this heap and the final heap you get.
b) Add a new node with the key value 16 to the heap created in question a). Show all steps and the final
heap you get for full marks.
c) Delete the node with the key value 16 from the heap constructed in question b). Show all steps and the
final heap you get for full marks.
13) Given a Binary Search Tree (BST) whose nodes are defined using the following class:
public class Node {
public int Key;
public String Value;
public Node Right;
public Node Left;
}
You can assume that there are no duplicate keys in the BST trees for the following questions.
a) Write a method NumOfLeaves() which returns the total number of leaves (a leaf is a node with no
child) in a BST tree.
public int NumOfLeaves(Node root)
b) Write a method Find2ndLargestKey() which finds out the node with the 2nd largest key and return this
key value in a BST. The total number of nodes, n, in the BST is given as an input. You can assume
that there are at least two nodes in the BST.
public int Find2ndLargestKey(Node root, int n)
14) Write a C++ program to read an postfix expression from the keyboard, evaluate its value, and print the
result. Your solution must use the ADT stack using the pointer-based implementation for the values in the
postfix expression (stack of integers). For simplicity you can assume that:
Implementation hints
Use the evaluate postfix algorithm given in class.
Sample run
This is a typical example of how your program should work (in interactive mode):
15)
a) Create a simple program to create a queue and to queue integer numbers from 1 to 30. Then dequeue
and print on screen the content of the queue. Use pointers for the ADT implementation.
i) Make an implementation within a single file and using only the main function (without using
clases)
ii) Make an implementation using one class and one main file.
iii) Make an implementation using as many classes as needed to create a whole object oriented
program.
b) Create a simple program to create an stack and to push integer numbers from 1 to 30. Then pop and
print on screen the content of the stack. Use pointers for the ADT implementation.
i) Make an implementation within a single file and using only the main function (without using
clases)
ii) Make an implementation using one class and one main file.
iii) Make an implementation using as many classes as needed to create a whole object oriented
program.
c)
i) Copy programs from (a) and (b) and modify them to print only odd numbers. Try to find two
approaches for both programs. Use pointers for the ADT implementation.
ii) Copy programs from (a) and (b) and modify them to print only numbers that could be divided by a
number provided by the user. Use pointers for the ADT implementation.
16)
a) Develop an array-implementation for a stack of characters in C++. Use it to build a function that
checks whether a string is a palindrome or not.
b) Develop a pointer-implementation for a stack of characters in C++. Use it to build a function that
checks whether a string is a palindrome or not.
c) You are developing the help desk application of a company. Request of issues will be stored to be
served on a first-come first-serviced basis. For each issue, the name of the employee and the
description will be stored. Implement the ADT queue on C++ for issues.