Академический Документы
Профессиональный Документы
Культура Документы
Algorithms
Contents
Contents
Abstract Data Types
Stack
Queue and Priority Queue
Linked Lists
Complexity Analysis
Hashing
Introduction to Graphs and Trees
Shortest Paths: Dijkstras Algorithm
Minimum Spanning Trees: Prims
Kruskals Algorithm
and
3
Stack
A stack is a linear data structure which
allows access to only one data item: the
last item inserted [1, 2].
The last tray put on the stack is the first
tray removed from the stack.
Stack is called an LIFO structure: Last
in First out.
Push
(10)
Pop()
Push (5)
Push
(15)
Push (7)
Pop ()
7
5
10
10
10
15
15
15
10
10
10
Contents
Abstract Data Types
Stack
Queue and Priority Queue
Linked Lists
Complexity Analysis
Hashing
Introduction to Graphs and Trees
Shortest Paths: Dijkstras Algorithm
Minimum Spanning Trees: Prims
Kruskals Algorithm
and
7
Queue
A queue is simple a waiting line that
grows by adding elements to the end
and shrinks by taking elements from
the front.
Queue is an FIFO structure: First in First
out.
Push
(10)
Push (5)
10
1
0
Pop()
Push
(15)
Push (7)
5 15
5 1
5
Pop ()
1
5
10
Priority Queue
Priority queue is exactly like a regular
queueor stackdata structure, but
additionally, each element is associated
with a priority.
Priority queue: elements are pulled
highest-priority or lowest-priority first.
11
501
43
10
9
30
2
63
2
89
1
Front
Rear
43
10
9
Front
10
9
30
2
30
2
50
1
50
1
Rear
63
2
63
2
89
1
89
1
Front
Rear
12
Contents
Abstract Data Types
Stack
Queue and Priority Queue
Linked Lists
Complexity Analysis
Hashing
Introduction to Graphs and Trees
Shortest Paths: Dijkstras Algorithm
Minimum Spanning Trees: Prims
Kruskals Algorithm
and
13
Linked Lists
A linked list is a collection of nodes
storing data and links to other nodes.
Nodes can be located anywhere in
memory, and linked from one node to
another by storing the address.
14
Links in a list
Linked
List
first
Link
Link
Link
Link
Dat
a
Dat
a
Dat
a
Dat
a
Nex
t
Nex
t
Nex
t
Nex
t
Null
15
Link
Data
first
a. Before
Insertion
Linked
List
Link
Data
Link
Dat
a
Next
Next
Next
Next
Link
Link
Dat
a
Link
Link
Dat
a
Data
first
Link
Dat
a
Next
Next
Data
Next
Next
Nul
l
Nul
l
Data
Next
b. After Insertion
16
Deleting a Link
Linked
List
first
Link
Data
Link
Dat
a
Link
Data
Link
Dat
a
Next
Next
Next
Next
Link
Link
Dat
a
Link
Link
Dat
a
Nul
l
a. Before
deleting
Linked
List
first
Next
Data
Next
Next
Nul
l
b. After deleting
17
Contents
18
Computational Complexity
A measure of resources in terms of Time and
Space
If A is an algorithm that solves a decision
problem f then run time of A is the number
of steps taken on the input of length n.
Time Complexity T(n) of a decision
problem f is the run time of the best
algorithm A for f.
Space Complexity S(n) of a decision
problem f is the amount of memory used by
the best algorithm A for f.
19
Big-O Notation
The Big-O indicates the time or the
memory needed.
The Big-O notation is used to give an
approximation to run-time-efficiency of
an algorithm.
20
21
Example
1-D array,
algorithm;
determine
the
Big-O
Line no
Instructions
line 1
sum = 0;
line 2
for ( i = 0; i < n; +
+i )
n+1
line 3
sum += a[ I ];
line 4
of
an
No of execution steps
1
1
2n + 3
Contents
23
Hashing
Ahash
functionis
any
algorithmor
subroutinethat maps large data setsto
smaller data sets, called keys [4].
Keys
John Smith
Lisa Smith
Same Doe
Sandra Dee
24
Hash Function
Hash Table
0: 360
1: 721
2: 2
3: 123
.
Array Index = key 10: 70
% 60
.
46: 226
.
.
59: 539
25
Contents
26
Graph
A simple graph G = (V, E) consists of a
nonempty set V of vertices and a
possibly empty set E of edges, each
edge being a set of two vertices from V.
V1
Edge (V1,
V2)
Vertex
V
V2
V3
27
28
Graph Representation
Graph G=(V, E) is a binary |V| |V|
matrix such that each entry of this
matrix:
1 if there exists an edge (ViVj)
aij=
0 otherwise.
29
Example
1
2
1
1 0
A= 2
3
3 0
2
0
1
1
3
1
0 1
0
30
Tree
Atree structureis a way of representing
the hierarchicalnature of a structurein a
graphical form [4].
Terms are used to describe in trees:
root: the node at the top of the tree.
parent: is a node one step higher in the
hierarchy and lying on the same branch.
leaf: a node that has no children.
level: level of a particular node refers to how
many generations the node is from the root.
31
Tree Term
root
Level 0
Level 1
Level 2
leaf
Level 3
32
Contents
33
Shortest Path
Shortest path problemis the problem of
finding a pathbetween two nodes such
that the sum of the weightsof its
constituent edges is minimized [4].
An example is finding the quickest way to
get from one location to another on a road
map;
where node represent locations;
edges represent segments of road;
weighted by the time needed to travel that
segment.
34
// Initializations
dist[v] := infinity ;
v
4
previous[v] := undefined ;
end for ;
dist[source] := 0 ;
8
9
10
11
if dist[u] = infinity:
12
break ;
source
13
end if ;
14
remove u from Q ;
15
from Q.
16
17
18
dist[v] := alt ;
19
previous[v] := u ;
decrease-key v in Q;
20
21
// Relax (u,v,a)
end if ;
end for ;
end while ;
return dist[] ;
end Dijkstra.
35
Dijstras Algorithm
36
37
Contents
39
Prims Algorithm
Input: A non-empty connected weighted graph with
verticesVand edgesE.
Initialize:Vnew= {x}, wherexis an arbitrary node (starting
point) fromV,Enew= {}
Repeat untilVnew=V:
- Choose an edge (u,v) with minimal weight such
thatuis inVnewandvis not
- AddvtoVnew, and (u,v) toEnew
Output:VnewandEnewdescribe a minimal spanning tree
40
41
Kruskals Algorithm
create a forestF(a set of trees), where each vertex in the graph is a
separate tree
create a setScontaining all the edges in the graph
whileSis nonemptyand F is not yet spanning
remove an edge with minimum weight fromS
if that edge connects two different trees, then add it to the
forest, combining two trees into a single tree
otherwise discard that edge.
42
http://www.unf.edu/~
wkloster/foundations/KruskalApplet/K
ruskalApplet.htm
http://www.math.ucsd.edu/~fan/algo
/CS101.swf
43
References
1. Drozdek A. Data Structure and
Algorithms in C++, 2nd Edition
2001.
2. Lafore R. Data Structures and
Algorithms in 24 hours 1999.
3. Parker A. Algorithms and Data
Structures in C++ 1993.
4. http://en.wikipedia.org
44