Boundary Conditions
No progress
toward goal:
Loops Forever!
Another bug!
mid
key 25
3
5
6
13
18
CSE 2011
Prof. J. Elder
21
21
25
36
43
49
51
53
p +q
Select mid =
2
60
72
74
83
88
91
95
If key mid, If key > mid,
then key is in then key is in
right half.
Example
CSE 2011
Prof. J. Elder
- 201 -
Last Updated: 4/1/10 10:16 AM
Example
CSE 2011
Prof. J. Elder
- 202 -
Last Updated: 4/1/10 10:16 AM
Example
CSE 2011
Prof. J. Elder
- 203 -
Last Updated: 4/1/10 10:16 AM
Djikstras Algorithm Cannot Handle Negative Ed
Map Methods with Separate Chaining
Delegate operations to a list-based map at each cell:
Algorithm get(k):
Output: The value associated with the key k in the map, or null if there is no
entry with key equal to k in the map
return A[h(k)].get(k)
CSE 2011
Dijkstras algorithm
Dijkstras algorithm can be viewed as greedy, since it always
chooses the lightest vertex in V S to add to S.
CSE 2011
Prof. J. Elder
- 196 -
Last Updated: 4/1/10 10:16 AM
Dijkstras algorithm: Analysis
Analysis:
Using minheap, queue ope
Hash Codes
Memory address:
We reinterpret the memory address of the key object as an integer
(default hash code of all Java objects)
Does not work well when copies of the same object may be stored at
different locations.
Integer cast:
We reinterpret
Correctness: Path relaxation property
CSE 2011
Prof. J. Elder
- 191 -
Last Updated: 4/1/10 10:16 AM
Correctness of DAG Shortest Path Algorithm
Because we process vertices in topologically sorted
order, edges of any path are relaxed in order of
appearance
Hash Tables
A hash table is a data structure that can be used to
make map operations faster.
While worst-case is still O(n), average case is typically
O(1).
CSE 2011
Prof. J. Elder
- 11 -
Last Updated: 06/02/12 10:23 PM
Applications of Hash Tables
data
Example
CSE 2011
Prof. J. Elder
- 186 -
Last Updated: 4/1/10 10:16 AM
Example
CSE 2011
Prof. J. Elder
- 187 -
Last Updated: 4/1/10 10:16 AM
Example
CSE 2011
Prof. J. Elder
- 188 -
Last Updated: 4/1/10 10:16 AM
Example
CSE 2011
Prof. J. Elder
- 189 -
Last
A Simple List-Based Map
We could implement a map using an
unsorted list
We store the entries of the map in a doubly-linked
list S, in arbitrary order
S supports the node list ADT (Section 6.2)
nodes/positions
header
9c
5a
6b
trailer
8d
entries
CSE 2011
P
General single-source shortest-path strategy
1. Start by calling INIT-SINGLE-SOURCE
2. Relax Edges
Algorithms differ in the order in which edges are
taken and how many times each edge is relaxed.
CSE 2011
Prof. J. Elder
- 181 -
Last Updated: 4/1/10 10:16
Maps, Hash Tables and Dictionaries
Chapter 9
CSE 2011
Prof. J. Elder
-1-
Last Updated: 06/02/12 10:23 PM
Maps
A map models a searchable collection of key-value
entries
The main operations of a map are for searching,
inserting, and deleting items
Multip
Remove with Linear Probing
Suppose we receive a remove(44) Example:
message.
h(x) = x mod 13
What problem arises if we simply
Insert keys 18, 41, 22, 44,
remove the key = 44 entry?
59, 32, 31, 73, in this order
k h(k)
i
18
5
5
41
2
2
22
9
9
44
5
6
59
Correctness of Dijkstras Algorithm: Upper Bound Property
Upper Bound Property:
Proof:
A valid path from s to v!
CSE 2011
Prof. J. Elder
- 206 -
Last Updated: 4/1/10 10:16 AM
Correctness of Dijkstras Algorithm
Optimal substructure
property!
Handled
CSE 201
Performance of Hashing
In the worst case, searches, insertions and removals on a hash table
take O(n) time
The worst case occurs when all the keys inserted into the map collide
The load factor = n/N affects the performance of a hash table
For separate
Array-Based Representation of Binary Trees
nodes are stored in an array, using
a level-numbering scheme.
1
A
2
let rank(node) be defined as follows:
4
rank(root) = 1
B
D
56
3
7
if node is the left child of parent(node),
E
F
C
J
rank(node) = 2*rank(parent
Running Time
Binary search can interact poorly with the memory
hierarchy (i.e. caching), because of its random-access
nature.
It is common to abandon binary searching for linear
searching as soon as the size of the remaining span falls
below a small val
Decision Tree
Binary tree associated with a decision process
internal nodes: questions with yes/no answer
external nodes: decisions
Example: dining decision
Want a fast meal?
No
Yes
How about coffee?
Yes
Second Cup
CSE 2011
Prof. J. Elder
On expense a
Define Step
It is faster not to check if the middle element is the key.
Simply continue.
key 43
3
5
6
13
18
21
21
25
36
43
49
If key mid,
then key is in
left half.
CSE 2011
Prof. J. Elder
51
53
60
72
74
83
88
91
95
If key > mid,
then key is in
right hal
Position ADT
The Position ADT models the notion of place
within a data structure where a single object is
stored
It gives a unified view of diverse ways of storing
data, such as
a cell of an array
a node of a linked list
a node of a tree
Just one metho
Ordered Search Tables
Performance:
find takes O(log n) time, using binary search
insert takes O(n) time since in the worst case we have to shift n
items to make room for the new item
remove takes O(n) time since in the worst case we have to shift n
it
Trees
Chapter 7
CSE 2011
Prof. J. Elder
-1-
Last Updated: 12-01-24 11:27 AM
Graph
a
b Node ~ city or computer
Edge ~ road or data cable
c
Undirected or Directed
A surprisingly large number of computational
problems can be expressed as graph problems.
CSE
A List-Based Dictionary
A log file or audit trail is a dictionary implemented by means of an
unsorted sequence
We store the items of the dictionary in a sequence (based on a doublylinked list or array), in arbitrary order
Performance:
insert takes O(1
Lecture 4.
The Java Collections Framework
Chapters 6.3-6.4
CSE 2011
Prof. J. Elder
-1-
Last Updated: 18/01/12 9:35 AM
Outline
Introduction to the Java Collections Framework
Iterators
Interfaces
Abstract Classes
Classes
CSE 2011
Prof. J. Elder
-2-
Las
Cycles
Shortest paths cant contain cycles:
Already ruled out negative-weight cycles.
Positive-weight: we can get a shorter path by omitting the cycle.
Zero-weight: no reason to use them
wont use them.
CSE 2011
Prof. J. Elder
- 176 -
assume that our soluti
Summary
We have covered the following linear data structures:
Arrays
Stacks
Queues
Singly-Linked Lists
Doubly-Linked Lists
CSE 2011
Prof. J. Elder
- 66 -
Last Updated: 12-01-17 9:52 AM
Summary
Based on this lecture, you should:
Know the basic line
Shortest Path Properties
Property 1 (Optimal Substructure):
A subpath of a shortest path is itself a shortest path
Property 2 (Shortest Path Tree):
There is a tree of shortest paths from a start vertex to all the other vertices
Example:
Tree of shortest p
BFS
s d=0
Found
Not Handled
Queue
d=1
b
a
d=1
e
d
g
c
f
i
h
g
b
c d=2
j
f
d=2 m
e
m
k
CSE 2011
Prof. J. Elder
l
- 136 -
Last Updated: 4/1/10 10:16 AM
BFS
s d=0
Found
Not Handled
Queue
d=1
b
a
d=1
e
d
g
c
b
c d=2
j
f
d=2 m
e
j
f
i
h
m
k
CSE 2011
Prof. J. E
Doubling Strategy Analysis
We replace the array k = log2 n times
The total time T(n) of a series of n add(o)
operations is proportional to
n + 1 + 2 + 4 + 8 + + 2k = n + 2k + 1 -1 = 3n -1
geometric series
Thus T(n) is O(n)
2
The amortized time of an a
BFS
Found
Not Handled
Queue
First-In First-Out (FIFO) queue
stores just discovered vertices
s
b
a
e
d
g
c
f
j
i
h
m
k
CSE 2011
Prof. J. Elder
l
- 131 -
Last Updated: 4/1/10 10:16 AM
BFS
Found
Not Handled
Queue
s d=0
b
a
s d=0
e
d
g
c
f
j
i
h
m
k
CSE 2011
package A1Q1;
import java.util.Iterator;
/*
* Represents a sparse numeric vector. Elements are comprised of a (long)
* location index an a (double) value. The vector is maintained in increasing
* order of location index, which facilitates numeric operatio
York University
EECS 2011E Fall 2015 Midterm Tuesday Oct 27th
Instructor: James Elder
1. (3 + 3 + 5 + 5 = 16 marks)
Let f (n) and g(n) be functions of n.
(a) In one line, give the mathematical definition of the statement f (n) (g(n).
Answer:
f (n) (g(n)
York University
EECS 2011E Fall 2015 Problem Set 1
Instructor: James Elder
This problem set will not be graded, but will help you understand asymptotic analysis of running times,
and thus prepare for the midterm. You are free to work together on these if