For quizzes: write R or L on the topleft corner.
Graphs and BFS, DFS, minimum spanning tree and shortest path.
Studyguide:
1.Givenagraph,drawitsadjacencymatrixandadjacencylistsdatastructures.
2.Givenagraph,tracethebreadthfirstsearchanddepthfirstsear
CS 360: Data Structures and Algorithms
Spring 2013
All-Pairs Shortest Paths:
Given a weighted (undirected or directed) graph,
find a minimum-distance path from each start
vertex to each destination vertex
[Restriction: here we allow edges with negative we
CS 360: Data Structures and Algorithms
Spring 2013
Dynamic Programming:
Example:
Floyds all-pairs shortest-paths algorithm
Format of dynamic programming algorithms:
Use a recursive formula
But recursion can be inefficient because it might
repeat the sam
CS 360: Data Structures and Algorithms
Spring 2013
Single-Source Shortest Paths:
Given a weighted (undirected or directed) graph and
a specified start vertex S, find a minimum-distance
path from S to each destination vertex
[Restriction: every edge weight
CS 360: Data Structures and Algorithms
Spring 2013
Divide-and-Conquer algorithms:
Examples:
Merge sort
Quick sort
Binary search
Format of divide-and-conquer algorithms:
Divide: Split the array or list into smaller pieces
Conquer: Solve the same problem
CS 360: Data Structures and Algorithms
Spring 2013
Greedy algorithms:
Examples:
Kruskals MST algorithm
Prims MST algorithm
Dijkstras SSSP algorithm
Selection sort
Insertion sort
Format of greedy algorithms:
for each item X in some order
decide whether/how
CS 360: Data Structures and Algorithms
Spring 2013
Minimum Spanning Tree:
Given a connected weighted undirected graph, find a
minimum-weight set of edges that forms a subtree
that connects all the vertices of the graph
Example:
A
15
16
14
13
2
E
D
6
G
MST
For quizzes: write R or L on the topleft corner.
Sorting in O(n^2), O(n lgn), and O(n) time.
Studyguide:
1Traceseveralsortingalgorithmsonagiveninputarray.(suchasinsertionsort,
selectionsort,bubblesort,mergesort,quicksort,heapsort,binsort,counting
so
CS 360 Quiz 13
Spring 2013
Solution
1. Given an initially empty min-ordered leftist heap. First insert these keys in the order shown:
5, 8, 2, 6, 4, 3, 7. Next call removeMin three times. Redraw the leftist heap at the end of each
insert or removeMin oper
CS 360 Quiz 12
Spring 2013
Solution
Given an initially empty min-ordered binary heap. First insert these keys in the order shown:
9, 8, 5, 7, 3, 6, 2, 4. Next call removeMin repeatedly until the heap becomes empty again.
Draw the binary tree representatio
CS 360 Quiz 11
Spring 2013
Solution
1. Given an initially empty hash table with capacity 13 and hash function H (x) = x % 13, insert
these values in the order given: 20, 32, 46, 71, 72, 45.
a. Separate
chaining
0
1
2
3
4
5
6 32,71,45
7 20,46,72
8
9
10
11
CS 360 Quiz 9
Spring 2013
P
D
Solution
1. Redraw this splay tree after finding key N.
B
B
A
D
C
A
N
X
B
Z
G
E
A
T
J
X
D
C
V
G
K
L
J
R
P
N
H
V
P
E
L
H
Z
T
B
E
C
A
D
C
class Node cfw_
ElementType data;
Node parent, left, right;
class BinaryTree cfw_
Node r
CS 360 Quiz 8
Spring 2013
Solution
Start with an initially empty AVL tree. Insert these keys in the given order: 9, 8, 7, 6, 5, 4, 3, 2, 1, 0.
Draw the AVL tree exactly as it should appear at the end of each insert operation. Label each node
with the pair
CS 360 Quiz 7
Spring 2013
Solution
1. Given an initially empty binary search tree. First insert these keys in the order shown:
7, 3, 11, 8, 6, 10, 4, 14, 0, 12, 2, 9, 5, 13, 1. Draw the tree after the final insert. Next
remove these keys in the order show
CS 360 Quiz 6
Spring 2013
Solution
1. Write the order that the nodes of this tree are visited by each traversal shown below.
H
L
C
J
B
Q
M
G
T
O
U
F
A
P
I
D
N
S
R
K
E
a. Preorder traversal: H L J O B U D N F S Q C M G A R P K E T I
b. Postorder traversal:
CS 360: Data Structures and Algorithms
Spring 2013
Depth-First Search in Graphs:
DFS is most similar to pre-order traversal of a tree,
and can also be similar to post-traversal traversal
boolean seen[1n];
DFS (Graph G) cfw_
for (k = 1; k<=n; k+)
seen[k] =
CS 360: Data Structures and Algorithms
Spring 2013
Skew heaps:
Skew heap = simpler variation of Leftist heap
(no rank information)
[Heap-ordering property]
The key at each node is the keys of its children
(assuming min-ordered heap)
Mergeable priority que
CS 360: Data Structures and Algorithms
Spring 2013
Leftist heap = Binary tree with these two properties:
[Heap-ordering property]
The key at each node is the keys of its children
(assuming min-ordered heap)
[Leftist property]
First define the rank of each
CS 360: Data Structures and Algorithms
Spring 2013
Priority Queue ADT:
Min-ordered PQ:
insert (x)
removeMin( )
Max-ordered PQ:
insert (x)
removeMax( )
Data structures for PQ (assume min-ordered):
Unsorted linked list
insert (1) time
removeMin (n) time
Sor
For quizzes: write R or L on the top-left corner.
Mach 20, course drop w/o penalty.
Graphs and BFS, DFS, minimum spanning tree and shortest path.
Study guide:
1. Given a graph, draw its adjacency matrix and adjacency lists data structures.
2. Given a grap
For quizzes: write R or L on the top-left corner.
Greedy algorithms, divide-and-conquer
Study guide:
1. Trace a greedy algorithm.
2. Determine whether a proposed greedy algorithm is correct or not.
3. Write a greedy algorithm for a specified problem.
4. S
For quizzes: write R or L on the topleft corner.
Disjoint Sets.
Studyguide:
1TraceasequenceofMergeandFindoperationsonadisjointsetsdatastructure.
2Writeapseudocodefunctionoralgorithmrelatedtodisjointsets.
3Analyzetherunningtimeofanalgorithmondisjoint
For quizzes: write R or L on the top-left corner.
Graphs and BFS, DFS, minimum spanning tree and shortest path.
Study guide:
1. Given a graph, draw its adjacency matrix and adjacency lists data structures.
2. Given a graph, trace the breadth-first search