Академический Документы
Профессиональный Документы
Культура Документы
Data structures
1. Introduction
2. Queues
3. Stacks
4. Linked lists
5. Trees
1
13-Nov-18
1. Introduction
Important problems types
Sorting, searching, string processing, graph
problems
Data structures
Essential to implement most algorithms
Data Structures + Algorithms = Programs
1. Introduction …contd
We focus on dynamic data structures
(which grow and shrink – unlike arrays)
Various structures exist – each with
specific features, pros and cons
Choose depending on suitability
Some data structures are so common
Stack, queue, list, tree, hash tables, priority
queue
4
2
13-Nov-18
1. Introduction …contd
1. Introduction …contd
3
13-Nov-18
1. Introduction …contd
2. Queues
A queue of customers waiting for services
Elements inserted (“enqueue”) at “tail”
Removed (“dequeue”) from “head”
FIFO (first in first out) structure
Useful in many situations
Processing jobs, print queues, messages
Other algorithms (e.g., graph traversal)
Special form: priority queue
8
4
13-Nov-18
2. Queues …contd
Array implementation
Queue has limited capacity
Implement as a “circular queue”
Maintain Q.capacity and Q.length
Algorithms for ENQUEUE(Q, x) and
DEQUEUE(Q)?
1 2 3 4 5 6 7 8 9
Q 11 7 18 14
head tail
9
2. Queues …contd
Pointer-based implementation
Each “object” has “value” and “next” fields
Dynamically create and delete objects
Write ENQUEUE(Q, x) and DEQUEUE(Q)
11 7 18 14
next next next next
NULL
head tail
10
5
13-Nov-18
2. Queues …contd
Priority queues
A data structure for maintaining a set of elements,
each associated with a key/priority, with the
following operations
Finding the element with the highest priority
Deleting the element with the highest priority
Inserting a new element
Scheduling jobs on a shared computer.
3. Stacks
A stack of plates
Elements inserted (“push”) at “top”
Removed (“pop”) from “top”
LIFO (last in first out) structure
Useful in many situations
Procedure calls
Interrupt processing in CPU
Other algorithms (e.g., graph traversal)
12
6
13-Nov-18
3. Stacks …contd
Array implementation
Usually has limited capacity
IsEMPTY(S)
Check if S.top = 0
Algorithms for PUSH(S, x) and POP(S)?
1 2 3 4 5 6 7 8 9
S 11 7 18 14
top 13
4. Linked Lists
Objects arranged linearly
“value” and “next” fields (at least) in each
Typically
implemented using pointers
insertions, deletions anywhere
Various applications
Lists of items
Available memory blocks in a computer
14
7
13-Nov-18
11 7 18 14
next next next next NULL
head 15
Sorted linked-list
7 11 14 18
next next next next NULL
8
13-Nov-18
Doubly-linked list
tail
head
11 7 18 14
next next next next
NULL prev prev prev prev NULL
Unsorted linked-list
SEARCH(L, x): O(n) where n is the list size
DELETE(L, x): O(n)
INSERT(L, x): O(1) if inserted at head
18
9
13-Nov-18
5. Graphs
Formal definition
A graph G = <V, E> is defined by a pair of two sets: a finite
set V of items called vertices and a set E of vertex pairs
called edges.
A graph is a collection of points called vertices, some of
which are connected by line segments called edges.
Undirected and directed graphs (digraph).
What’s the maximum number of edges in an
undirected graph with |V| vertices?
(a) (b)
Graphs
Complete, dense, and sparse graph
A graph with every pair of its vertices connected by an edge is
called complete.
Number of edges is close to the maximal number of edges is
called dense graph.
Number of edges is close to the minimal number of edges is
called sparse graph
20
10
13-Nov-18
Graph Problems
Modeling real-life problems
Modeling WWW
communication networks
Project scheduling …
Examples of graph algorithms
Graph traversal algorithms
Shortest-path algorithms
Topological sorting
Graph Representation
Adjacency matrix
n x n boolean matrix if |V| is n.
The element on the ith row and jth
column is 1 if there’s an edge from
ith vertex to the jth vertex;
otherwise 0.
The adjacency matrix of an
The image
cannot be
displayed. Your
computer may
not have enough
memory to open
the image, or the
11
13-Nov-18
5. Trees …contd
Trees
A tree (or free tree) is a connected acyclic graph.
Forests: a graph that has no cycles but is not necessarily
connected.
Properties of trees
|E| = |V| - 1
For every two vertices in a tree there always exists exactly one
simple path from one of these vertices to the other. Why?
Rooted trees: The above property makes it possible to select an
arbitrary vertex in a free tree and consider it as the root of the so-
called rooted tree.
Levels of rooted tree.
12
13-Nov-18
Rooted Trees
ancestors
For any vertex v in a tree T, all the vertices on the simple path from the
root to that vertex are called ancestors.
descendants
All the vertices for which a vertex v is an ancestor are said to be
descendants of v.
parent, child and siblings
If (u, v) is the last edge of the simple path from the root to vertex v (and u
v), u is said to be the parent of v and v is called a child of u.
Vertices that have the same parent are called siblings.
Leaves
A vertex without children is called a leaf.
Subtree
A vertex v with all its descendants is called the subtree of T rooted at v.
13
13-Nov-18
Trees
Depth of a vertex
The length of the simple path from the root to the vertex.
Height of a tree
The length of the longest simple path from the root to a leaf.
17 Depth 0
Height = 2 9 14 15 Depth 1
6 5 8
Depth 2
27
Ordered Trees
Ordered trees
An ordered tree is a rooted tree in which all the children of each vertex
are ordered.
Binary trees
A binary tree is an ordered tree in which every vertex has no more than
two children and each children is designated as either a left child or a
right child of its parent.
Binary search trees
Each vertex is assigned a number.
A number assigned to each parental vertex is larger than all the
numbers in its left subtree and smaller than all the numbers in its right
subtree.
log2n h n – 1, where h is the height of a binary tree.
14
13-Nov-18
Binary Trees
Binary trees: most common form
Each node has at most 2 children
“left child” “left subtree” “right child”
17 17
9 15 9 15
6 6 5 10
5 8
15
13-Nov-18
Binary Trees
How to implement a binary tree?
“node” object consisting of fields:
“value” etc.,
Pointers to right and left children (to parent?)
31
32
16
13-Nov-18
17
Example binary
search tree
9 19
6 17 25
33
17
13-Nov-18
9 15
6 5 10
8
6, 9, 5, 17, 8, 15, 10
35
Sorting
Rearrange the items of a given list in scending/descending
order.
Input: A sequence of n numbers <a1, a2, …, an>
Output: A reordering <a´1, a´2, …, a´n> of the input sequence such
that a´1≤ a´2 ≤ … ≤ a´n.
Why sorting? Help searching
Examples of sorting algorithms
Bubble sort
Selection sort
Insertion sort
Merge sort
Quick sort
Heap sort …
18
13-Nov-18
Searching
Find a given value, called a search key, in a given set.
Examples of searching algorithms
Sequential searching
Binary searching…
String Processing
A string is a sequence of characters from
an alphabet.
Text strings: letters, numbers, and special
characters.
String matching: searching for a given
word/pattern in a text.
19