Академический Документы
Профессиональный Документы
Культура Документы
Data Structures and Algorithms in C++, Second Edition by Adam Drozdek, published by Brooks/Cole
Thomson Learning, © 2001, ISBN 0-534-37597-9
KRISNA ADIYARTA
PASCA SARJANA (MAGISTER KOMPUTER)
UNIVERSITAS BUDI LUHUR
JAKARTA
Definitions
A data structure is a scheme for organizing data in
the memory of a computer.
The way in which the data is organized affects the
performance of a program for different tasks.
Computer programmers decide which data
structures to use based on the nature of the data
and the processes that need to be performed on
that data.
Some of the more commonly used data structures
include : primitive data, arrays, lists, stacks,
queues, trees, graphs
Primitive (Atomic) Structures
Number
Integer
Floating-Point
Boolean
Character
ASCII, ISO-8859-x, JIS, UNICODE
User-Defined Ordinal Type
Design issues:
Should strings be a special kind of character
array? Or a primitive type?
Static or dynamic length?
Pointer Type
struct {
Person
char name[10];
int age;
int salary;
} person;
strcpy(person.name, “james”);
person.age=10; Nam e Salary
Age
person.salary=3000;
Array Structure
HEAD CURRENT
A B C D
Insertion
A B C
X
2
A B C
3 1
X
A B X C
Deletion
A B X
C D
1
A B D
2
C
A B D
Double Linked List
A doubly linked list provides a natural
implementation of the List
Special trailer and header nodes
Nodes implement Position and store:
element prev next
link to the previous node
link to the next node
elem node
A B C
p
A B C
q
X
p q
A B X C
Deletion
A B C D
A B C
p
D
A B C
Tree
A tree is a finite nonempty set
of elements.
It is an abstract model of a
hierarchical structure.
consists of nodes with a
parent-child relation. Computers”R”Us
Applications:
Organization charts
File systems Sales Manufacturing R&D
Programming environments
Data
A D F
C E
C E
Left Child, Right Sibling Representation
Data
Left Right
Child Sibling A
B C D
E F G H I
J K L
Tree Traversal
Two main methods:
Preorder
Postorder
Preorder
visit the root
traverse in preorder the children (subtrees)
Postorder:
traverse in postorder the children (subtrees)
visit the root
Preorder Traversal
A traversal visits the nodes of a Algorithm preOrder(v)
tree in a systematic manner visit(v)
In a preorder traversal, a node is for each child w of v
visited before its descendants preorder (w)
Application: print a structured
document
1
Become Rich
2 5 9
1. Motivations 2. Methods 3. Success Stories
3 4 6 7 8
1.1 Enjoy 1.2 Help 2.1 Get a 2.2 Start a 2.3 Acquired
Life Poor Friends CS PhD Web Site by Google
Postorder Traversal
In a postorder traversal, a Algorithm postOrder(v)
node is visited after its for each child w of v
descendants postOrder (w)
Application: compute space visit(v)
used by files in a directory and
its subdirectories
9
cs16/
8
3 7
todo.txt
homeworks/ programs/
1K
1 2 4 5 6
h1c.doc h1nc.doc DDR.java Stocks.java Robot.java
3K 2K 10K 25K 20K
Decision Tree
Binary tree associated with a decision process
internal nodes: questions with yes/no answer
external nodes: decisions
A 1 A
A
B B 2 B C
C
3 D E F G
D
4 H I
E 5
Differences Between A Tree and A Binary Tree
A A
B B
B A D
A D
C E
C E
Arithmetic Expression Tree
+
2 - 3 b
a 1
Maximum Number of Nodes in a Binary Tree
Prove by induction.
k
2
i 0
i
2 k +1
-1
Full Binary Tree
A full binary tree of a given height k has 2k+1–1
nodes.
2 3
4 5 6 7
8 9 10 11 12 13 14 15
Node Number Properties
1
2 3
4 5 6 7
8 9 10 11 12 13 14 15
2 3
4 5 6 7
8 9 10 11 12 13 14 15
2 3
4 5 6 7
8 9 10 11 12 13 14 15
1 1
2 2 3
3
4 5 6 7 4 5 6 7
8 9 10 11 12 13 14 15
8 9
Complete binary tree Full binary tree of depth 3
Binary Tree Traversals
Let l, R, and r stand for moving left, visiting
the node, and moving right.
2 8
1 4 7 9
3 5
Graph
A graph, G=(V, E), consists of two sets:
a finite set of vertices(V), and
a finite, possibly empty set of edges(E)
V(G) and E(G) represent the sets of vertices and edges of G,
respectively
Undirected graph
The pairs of vertices representing any edges is unordered
e.g., (v0, v1) and (v1, v0) represent the same edge
Directed graph
Each edge as a directed pair of vertices
e.g. <v0, v1> represents an edge, v0 is the tail and v1 is the
head
Graph
0 0 0
1 2 1 2
1
3
3 4 5 6
G1 2
G2 incomplete graph
complete graph G3
V(G1)={0,1,2,3} E(G1)={(0,1),(0,2),(0,3),(1,2),(1,3),(2,3)}
V(G2)={0,1,2,3,4,5,6} E(G2)={(0,1),(0,2),(1,3),(1,4),(2,5),(2,6)}
V(G3)={0,1,2} E(G3)={<0,1>,<1,0>,<1,2>}
complete undirected graph: n(n-1)/2 edges
complete directed graph: n(n-1) edges
Complete Graph
0 0 0 1 2 0
1 2 1 2 3 1 2
3
3
G1
(i) (ii) (iii) (iv)
(a) Some of the subgraph of G1
Simple Path and Style
A simple path is a path in which all vertices,
except possibly the first and the last, are distinct
A cycle is a simple path in which the first and
the last vertices are the same
In an undirected graph G, two vertices, v0 and v1,
are connected if there is a path in G from v0 to v1
An undirected graph is connected if, for every
pair of distinct vertices vi, vj, there is a path
from vi to vj
Simple Path and Style
connected
0 0
1 2 1 2
3
3 4 5 6
G1
G2
tree (acyclic graph)
Connected Component
undirected graph
directed graph
degree
3 in-degree 0 in:1, out: 1
0 out-degree
3 1 2 3 1 in: 1, out: 2
3
3 2 in: 1, out: 0
G1 G2
Adjacency Matrix
Let G=(V,E) be a graph with n vertices.
The adjacency matrix of G is a two-dimensional
n by n array, say adj_mat
If the edge (vi, vj) is in E(G), adj_mat[i][j]=1
If there is no such edge in E(G), adj_mat[i][j]=0
The adjacency matrix for an undirected graph is
symmetric; the adjacency matrix for a digraph
need not be symmetric
Adjacency Matrix
0 0 0 4
2 1 5
1 2
1 3 6
3
0 1 1 1 2 G3 7
1 0 1 1
1 1 0 1 0 1 0 0 1 1 0 0 0 0 0
1
1 1 1 0 1 0 1
0 0 1 0 0 0 0
0 0 0
1 0 0 1 0 0 0 0
0 1 1 0 0 0 0 0
G 0 0 0 0 0 1 0 0
1 G2
0 0 0 0 1 0 1
0
0 0 0 0 0 1 0 1
undirected: n2/2
0 0 0 0 0 0 1 0
symmetric
directed: n2
Adjacency lists
N linked list
0 3 1 2
1 0
2 3 0
1
2 0 1 2
1 3 0
3
2 0 1 2
G2
G1
Graph Operations
Traversal
Given G=(V,E) and vertex v, find all wV,
such that w connects v.
Depth First Search (DFS)
preorder tree traversal
Breadth First Search (BFS)
level order tree traversal
Spanning Trees
Graph Operations
depth first search: v0, v1, v3, v7, v4, v5, v2, v6
breadth first search: v0, v1, v2, v3, v4, v5, v6, v7
Weighted Graph
In Many applications,each edge of a graph has an
associated numerical value, called a weight
Usually, the edge weights are non negative
integers
Weight graphs may either directed or undirected
Spanning Trees
A spanning tree is a minimal subgraph G’, such
that V(G’)=V(G) and G’ is connected
Weight and MST
Either dfs or bfs can be used to create a
spanning tree
When dfs is used, the resulting spanning tree is
known as a depth first spanning tree
When bfs is used, the resulting spanning tree is
known as a breadth first spanning tree
Minimum-Cost Spanning Trees
1 6 1
14
1 2 5 6 2
16
0 0
3 6 28
4
18
10 1 10 1
3 4 14 16 3
22
5 6 2 5 6 2
4 24 6 24
25 18 12
4 5 4 4
25 22 3 3
0 28 1
0 10 5 Examples for Kruskal’s Algorithm
2 12 3
1 14 6
1 16 2 0 0 0
18 10 1 10 1 10 1
3 6
14 14
3 22 4 5 6 2 5 6 2 5 6 2
12 12 12
4 24 6 4 4 4
3 3 3
4 25 5
0 28 1
0 10 5 Examples for Kruskal’s Algorithm
2 12 3
1 14 6
0 0
1 16 2
10 1 10 1
3 18 6 14 16 14 16
5 6 2 5 6 2
3 22 4
25
12 12
4 4
4 24 6 22
22 3
3
4 25 5 cycle
cost = 10 +25+22+12+16+14
0 28 1
0 Examples for Prim’s
28
10 1
Algorithm
14 16
5 6 2
24
25
18 12
4
22 3
0 0 0
10 1 10 1 10 1
5 6 2 5 6 2 5 6 2
25 25
4 4 4
3 22 3
3
0 Examples for Prim’s
28
10 1
Algorithm
14 16
5 6 2
24
25
18 12
4
0 22 3 0 0
10 1 10 1 10 1
16 14 16
5 6 2 5 6 2 5 6 2
25 25 25
12 12 12
4 4 4
22 22 22 3
3 3