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

13-Nov-18

Design and Analysis of


Algorithms
Review of Data Structures

Dr. Yasir Mehmood


Assistant Professor

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

 Let S be the collection, x be an element


 Basic operations need to be supported
 SEARCH(S, x)
 INSERT(S, x)
 DELETE(S, x)
 Other possible operations
 MINIMUM(S), MAXIMUM(S), IsEMPTY(S),
PRINT(S), COUNT(S)
5

1. Introduction …contd

 Pointers useful for some data structures


 Some languages support, some not
 E.g., C supports, Java uses “references”

 A pointer is a memory address


 E.g., pointer to an integer, char, object etc.,
Memory address 1020 5

Memory address 4064 contains a pointer


4064 1020
6

3
13-Nov-18

1. Introduction …contd

 Note that the object pointed by a pointer


may occupy a range of memory locations
x Dot “.” notation
(1) x.value,
value
y.name …
y name (2) y ← x.next
… Arrow/pointer “->” notation
… (1) x->value,
y->name …
next
(2) y ← x-
>next
7

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

4. Linked Lists …contd

 Different forms: Unsorted (typical)


 Can you write:
 SEARCH(L, x), INSERT(L, x), DELETE(L, x) ?

11 7 18 14
next next next next NULL

head 15

4. Linked Lists …contd

Sorted linked-list

7 11 14 18
next next next next NULL

head Can you write algorithms for:


- SEARCH, INSERT, DELETE ?
16

8
13-Nov-18

4. Linked Lists …contd

Doubly-linked list
tail
head

11 7 18 14
next next next next
NULL prev prev prev prev NULL

Can you write algorithms for:


- SEARCH, INSERT, DELETE ?
17

4. Linked Lists …contd

 Can you estimate complexities of the


algorithms?
 SEARCH(L, x), INSERT(L, x), DELETE(L, x)

 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

undirected graph is symmetric.


image may hav e
been corrupted.
Restart your
computer, and
then open the file
again. If the red
x still appears,
y ou may hav e to
delete the image
and then insert it

 Adjacency linked lists again.

 A collection of linked lists, one for


each vertex, that contain all the
vertices adjacent to the list’s
vertex.

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.

A tree and a forest

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.

Transformation of a free tree into a rooted tree

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

A binary tree and a binary search tree

Standard implementation of (b)

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

Example binary trees


30

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?)

 On special cases, can implement using


an array

31

Binary Search Trees


 A binary tree in which binary-search-tree
property holds
 Let x be a node in the tree
 If y is a node in the left subtree of x, then
y.value ≤ x.value
 If y is a node in the right subtree of x, then
x.value ≤ y.value

32

16
13-Nov-18

Binary Search Trees …contd

17
Example binary
search tree
9 19

6 17 25

If the tree is balanced, operations take O(lg n) time

33

Binary Tree Traversal


 How to visit (print) all nodes in a tree?
 One method is “inorder traversal”
Input: pointer/reference to root of tree
INORDER(x)
if x is not NULL
INORDER(x.left_child)
print x.value
INORDER(x.right_child)
34

17
13-Nov-18

Binary Tree Traversal


 Perform inorder traversal on this tree and
give the result
17

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

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