Вы находитесь на странице: 1из 44

Data Structure and

Algorithms

Contents

Abstract Data Types


Complexity Analysis
Hashing
Introduction to Graphs and Trees
Shortest Paths: Dijkstras Algorithm
Minimum Spanning Trees: Prims and
Kruskals Algorithm

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.

Operators of Stack in STL


Clear( ) Clear the stack.
empty() Check to see if the stack is
empty.
push(el) put the element el on the
topmost of the stack.
pop() Remove the topmost element
from the stack.
top() Return the topmost element in
the stack without removing.
5

Operation Executed on a Stack

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.

Queue Operations in STL


back() return the last element.
empty() return true if the queue
includes
no
element
and
false
otherwise.
front() return the first element.
pop() remove the first element.
push(el) insert el at the end of
queue.
size() return the number of elements.
9

Operation Executed on a Queue

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

Operations of Priority Queue


43

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

Inserting a New Link


Linked
List

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

Abstract Data Types


Complexity Analysis
Hashing
Introduction to Graphs and Trees
Shortest Paths: Dijkstras Algorithm
Minimum Spanning Trees: Prims and
Kruskals Algorithm

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

The Big-O of an Algorithm A

If an algorithm A requires time proportional to f(n),


then the algorithm A is said to be of order f(n),
and it is denoted as O(f(n)).
If an algorithm A requires time proportional to n2,
then order of the algorithm is said to be O(n2).
If an algorithm A requires time proportional to n,
then order of the algorithm is said to be O(n).

Similarly, for algorithms having performance


complexity O(log2(n)), O(n3),

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

cout << sum;


Total

Determine the Big-O:

of

an

No of execution steps
1

1
2n + 3

Ignoring constants such as 2 and 3, the algorithm


is of the order n.
The Big-O of algorithm is O(n)
22

Contents

Abstract Data Types


Complexity Analysis
Hashing
Introduction to Graphs and Trees
Shortest Paths: Dijkstras Algorithm
Minimum Spanning Trees: Prims and
Kruskals Algorithm

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

Hash Function Hashes


00
01
02
03
.
.
.
15

24

Example with Hashing

The range of keys runs from 0 to 999.


The initial size of the array is 60.
The hash function is used operator modulo (%) to squeeze
the range of keys down to match the array size.
Key
360
123
226
539
70
2
721

Hash Function

Hash Table

0: 360
1: 721
2: 2
3: 123
.
Array Index = key 10: 70
% 60
.
46: 226
.
.
59: 539
25

Contents

Abstract Data Types


Complexity Analysis
Hashing
Introduction to Graphs and Trees
Shortest Paths: Dijkstras Algorithm
Minimum Spanning Trees: Prims and
Kruskals Algorithm

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

Undirected and Directed Graphs

Undirected graphs which edges have no


orientation.

Directed graph is a graph with vertices and


edges where each edge has a specific
direction relative to each of the vertices.

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

Abstract Data Types


Complexity Analysis
Hashing
Introduction to Graphs and Trees
Shortest Paths: Dijkstras Algorithm
Minimum Spanning Trees: Prims and
Kruskals Algorithm

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

function Dijkstra(Graph, source):

for each vertex v in Graph:

// Initializations

dist[v] := infinity ;

// Unknown distance function from source to

v
4

previous[v] := undefined ;

end for ;

dist[source] := 0 ;

Q := the set of all nodes in Graph ;

8
9

// Previous node in optimal path from source


// Distance from source to source

// All nodes in the graph are unoptimized - thus are in Q


while Q is not empty:

10

// The main loop

u := vertex in Q with smallest distance in dist[] ;

11

if dist[u] = infinity:

12

break ;

// all remaining vertices are inaccessible from

source
13

end if ;

14

remove u from Q ;

15

for each neighbor v of u:

// where v has not yet been removed

from Q.
16

alt := dist[u] + dist_between(u, v) ;

17

if alt < dist[v]:

18

dist[v] := alt ;

19

previous[v] := u ;
decrease-key v in Q;

20
21

// Relax (u,v,a)

// Reorder v in the Queue

end if ;
end for ;
end while ;
return dist[] ;
end Dijkstra.

35

Dijstras Algorithm

36

Illustration of Dijkstras Algorithm

37

Contents

Abstract Data Types


Complexity Analysis
Hashing
Introduction to Graphs and Trees
Shortest Paths: Dijkstras Algorithm
Minimum Spanning Trees: Prims and
Kruskals Algorithm
Prims Algorithm
Kruskals Algorithm
38

Minimum Spanning Tree


Aminimum
spanning
treeorminimum weight spanning
treeis then a spanning tree with
weight less than or equal to the weight
of every other spanning tree [4].

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

Illustration Kruskals Algorithm

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

Вам также может понравиться