0% found this document useful (0 votes)
3 views

Algorithm Design

This document covers non-linear data structures, specifically trees and graphs, along with various algorithm design techniques such as greedy algorithms, divide and conquer, and dynamic programming. It explains tree properties, binary search trees, and their operations, as well as graph types and representations. The document concludes with a summary of the key concepts discussed in the module.

Uploaded by

harpreet1828
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Algorithm Design

This document covers non-linear data structures, specifically trees and graphs, along with various algorithm design techniques such as greedy algorithms, divide and conquer, and dynamic programming. It explains tree properties, binary search trees, and their operations, as well as graph types and representations. The document concludes with a summary of the key concepts discussed in the module.

Uploaded by

harpreet1828
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 44

NON-LINEAR DATA

STRUCTURE
AND
ALGORITHM DESIGN
TECHNIQUES
In this module we will discuss

• Non-Linear Data Structures


– Tree
– Graph

• Various Algorithm Design Techniques


– Greedy Algorithm
– Divide and Conquer
– Dynamic Programming
Tree
Tree

• Tree is a non-linear abstract data structure to model

hierarchical relationships

• A tree is a data structure consisting of nodes organized as

a hierarchy

• Tree is an acyclic directed graph

• A tree is a finite nonempty set of elements

• It consists of nodes with a parent-child relation.

- A root node with sub trees (child nodes) attached to it


Tree definition: - Each node is the root of a sub tree
Tree Terminologies

Root node
Child node
Application of Tree data structure

• For creating Organization charts


• To store large volumes of data
• Used in compilers: expression tree , symbol tree
• To implement data base management systems
• Tree Data structure is used in File Systems: Directory Hierarchies

Organization chart
Tree Representation

How many link fields are needed


in such a representation?
Data Link 1 Link 2 … Link n

The root comes first, followed by a list of links to sub-trees


Binary Trees

A binary tree is one in which each node has a maximum of two children.
Thus the node can have one child or two children or none at all.

The children of a node are an


ordered pair
Binary Trees variations
• Complete binary tree, every level is
completely filled, except possibly the last, • In Fully binary tree, every node other
and all nodes are to the far left as much as than the leaves has two children
possible

• The maximum number of nodes on depth i of a binary tree is 2i, i>=0.


• The maximum number of nodes in a binary tree of height k is 2k+1-1, k>=0.
Binary Search Tree

BST is a collection of nodes arranged in a way where they maintain BST properties.

BST properties:
• Every node in the left subtree has a key, whose value is
less than the value of its parent node’s key value

• Every node in the right subtree has a key, whose value


is greater than or equal to the value of its parent
node’s key value
Why BST?

• In Binary Search Tree, numbers have been stored in a specific order.


• The value at a node is more than the value stored in the left-child node, and less than
the value stored in the right-child node.
• Uses divide and conquer strategy for search queries.

This tree arrangement, reduced the search time


Searching time: O(h)
BST Operations
Major operations are:

Search − Searches an element in a tree.


Insert − Inserts an element in a tree.
Pre-order Traversal − Traverses a tree in a pre-order manner.
In-order Traversal − Traverses a tree in an in-order manner.
Post-order Traversal − Traverses a tree in a post-order manner.

Tree Node – Implementation :

• Implement a tree node using structure.


• A tree node contains some data and references to its left and right child node

class TreeNode {
int data;
TreeNode left, right;
}
Search
Algorithm for search an element in BST
Iterative-Tree-Search(x, k)
start at the root
1. while x  NIL and k  key[x]
REPEAT until you reach a terminal node
2. do if k < key[x]
IF value at the node = search value THEN
3. then x  left[x]
found the element and return
4. else x  right[x]
IF value at node < search value THEN
5. return x
move to left descendant
ELSE move to right descendant
END REPEAT Recursive-Tree-Search(x, k)
1. if x = NIL or k = key[x]
2. then return x
3. if k < key[x]
k is the key that is searched for and 4. then return Tree-Search(left[x], k)
x is the start node 5. else return Tree-Search(right[x], k)
Insertion

To insert a new node:


• First find its proper location
• Start searching from the root node, if the data is less than the key value, search for the
empty location in the left subtree and insert the data
• Otherwise, search for the empty location in the right subtree and insert the data
Algorithm for Insertion in BST

treeInsert(int data, TreeNode Root) {


if Root is NULL then
Create the new node and set as Root
set the data to Root.data;
Set Root.left = NULL; Allocate Memory for the
Set Root.right = NULL; new node and load the
else if data is less than Root.data then data into it
set Root.left = treeInsert(data, Root.left);
(Then node needs to be inserted in left sub-tree. So,
recursively traverse left sub-tree to find the place
where the new node needs to be inserted)
else if data is greater than Root.data then
Root.right = treeInsert(data, Root.right);
(Then node needs to be inserted in right sub-tree
So, recursively traverse right sub-tree to find the
place where the new node needs to be inserted)
}
Tree Traversal in BST

Preorder traversal:
The order of visit is: The current, then the left subtree node, then
the right subtree nodes
Thus the root is present in-between the left and right subtrees.

Inorder traversal:
The order of visit is: The left subtree nodes, the current node,
then the right subtree nodes.
Thus the root is present before the left and right subtree nodes

Postorder traversal:
The order of visit is: The left subtree nodes, the right subtree
nodes, then the current node
Thus the root is present after the left and right subtree nodes
Preorder traversal algorithm

Preorder Traversal:
• Visit and print the root node
• Recursively traverse left sub-tree(in pre-order)
• Recursively traverse right sub-tree(in pre-order)

Preorder traversal for the above tree is : a b c d f g e


Inorder traversal algorithm

Inorder Traversal:
• Recursively traverse left subtree(inorder)
• Visit and print the root node
• Recursively traverse right subtree(inorder)

Inorder traversal for the above tree is : b a f d g c e


Postorder traversal algorithm

Postorder Traversal:
• Recursively traverse left subtree (in post order)
• Recursively traverse right subtree (in post order)
• Visit and print the root node

Postorder traversal for the above tree is : b f g d e c a


Introduction to Graph

Find all nodes in the graph


having length of the shortest
path from Rama, equal to 2

Ella, Bob and Katie


Introduction to Graph
Graph Definition

The edges of a graph provide the


connections between one node
and another.
By default, an edge is assumed to
be bidirectional.
Applications of Graph
Graph variations
Undirected Graphs.
• Edges have no direction. They are not arrows but straight lines.
• The order of the vertices in the pairs in the edge set doesn't matter.
Can traverse edges in either directions.
• In the sample graph given, we could write the Edge set as (4,6),(4,5),
(3,4),(3,2),(2,5)),(1,2)),(1,5)}.

Directed Graphs.
• A graph whose edges are directed (i.e. have a direction). An arrow
from u to v is drawn only if (u,v) is in the Edge set.
• In a directed graph the order of the vertices in the pairs in the edge
set matters.
• The Edge set = {(A,B),(B,C),(D,C), (B,D),(D,B) ,(E,D),(B,E)}
Graph variations

Weighted Graph : Some weight or cost is given to the edges.


Airline Example: Imagine a network that shows varying routes for flights. Vertices represent the cities
and the edges represent a possible route from city to city. Weight represents the cost of travel.

From this network you


can decipher the
cheapest flights from San
Francisco to Singapore.

Unweighted Graph: edges have no weight


Graph Variations
cycle? Simple path that starts and ends at the same vertex or node

Cyclic Graph : A graph contains cycles or closed regions Acyclic Graph: A graph contains no cycles

An Acyclic Directed Graph is a Tree


Most commonly used representations of graph:
Graph Representations
Adjacency Matrix

Adjacency List

Incidence Matrix Incidence List

Adjacency Matrix: Adjacency List:


Pros:
Pros:
• Easy to implement and follow.
• List allows us to store graph in a more compact
• Queries like whether there is an edge from
form, so saves space.
vertex ‘u’ to vertex ‘v’; are efficient and can be
• Adding a vertex is easier
done O(1)

Cons:
Cons: • Queries like whether there is an edge from
• Consumes huge amount of memory for storing
vertex u to vertex v; are not efficient and can
big graphs
be done O(V)
Graph Representations
Adjacency Matrix:
Adjacency Matrix is a 2D array of size V x V
Adjacency matrix for the
V is the number of vertices in a graph
given graph
Example:
Let the 2D array be graph[5][5]. A
slot graph[i][j] = 1 indicates that
there is an edge from vertex i to
vertex j

Adjacency matrix for undirected graph is always symmetric

Adjacency Matrix is also used to represent weighted graphs


If graph[i][j] = w, then there is an edge from vertex i to vertex j with weight w
Graph Representations
Adjacency List:
The adjacency list structure is simply a linked list version of the adjacency table
An array of linked lists is used. Size of the array is equal to number of vertices.

Example: Adjacency list representation of the given graph

Let the array be array[5].


An entry in array[i] represents the
linked list of vertices adjacent to the This representation can also be used to represent a weighted
ith vertex. graph. The weights of edges can be stored in nodes of linked lists.
Algorithm Design Techniques
Algorithm Design strategies are important to compare solutions

Analyze algorithms on the basis of design strategies helps to find proper


solution to a problem
There are different strategies and these strategies to be analyzed based on the complexity of the
algorithms
Various Design Strategies
Commonly used design strategies are:

Greedy Algorithm

Dynamic Programming

Divide and Conquer


Greedy algorithm
Little James is given a bunch of candies to choose from.

He repeatedly eats his favorite candy among the other candies, until
he’s satisfied or until his mother tells him to stop!

In many cases, like James’s candies, the


greedy algorithm turns out to be optimal!

Greedy Algorithm helps to select the best


solution from the available solutions
without thinking much.
Greedy algorithm
This algorithm is designed to achieve optimum solution for a given problem.
At each step, it quickly makes a choice that currently looks best.

This is a technique for solving optimization problems:


Maximize profit and minimize cost

The local optimal (greedy) choice Sub-problems are solved first. Greedy
choice can be made first before solving further sub-problems.

It takes decisions on the basis of information at hand without


worrying about the effect these decisions may have in the future.
Greedy algorithm

Greedy algorithms are simpler and more efficient when we compare with
other optimization problem solutions.
Examples for Greedy algorithm

Optimization problems appear in many applications :

• Encode the data in a file to minimize its size


[Huffman Encoding Problem]
• Collect the maximum value of goods that fit in a given bucket
[fractional knapsack Problem]
• Travelling sales man wants to minimize the total length of the trip [Travelling Salesman Problem]
• Count to a desired value by choosing the least possible coins [Coin Change Problem]
• Select the smallest-weight of edges to connect all nodes in a graph [Minimum Spanning Tree]
Divide and Conquer algorithm
Dividing the data of the problem is called divide and conquer technique

Partitioning of the data


into different groups

Keep dividing the group of


data until it becomes small
enough to solve

Find the solution individually


and combine the solutions to
get the global solutions
Divide and Conquer
Planning Construction

Project
Completion Assembly
Deployed
Examples of Divide and Conquer algorithm

Binary search

Merge sort

Quick sort
Dynamic Programming
Like Greedy algorithm, Dynamic Programming algorithm is also used to solve optimization problems

Like Divide and Conquer algorithm, Dynamic Programming is also an algorithmic paradigm that solves
a given complex problem by breaking it into similar
sub problems and stores the results of sub problems to avoid computing the same results again.

Before solving the in-hand sub-problem, dynamic algorithm will try to examine the results of the
previously solved sub-problems.

The solutions of sub-problems are combined in order to achieve the best solution.
Dynamic Programming
Dynamic Programming
Main concept:
• The problem should be able to divide itself into smaller overlapping sub-problems.
• Solve smaller instances once.
• Record solutions in an array/table (memorization).
• Extract solutions to the initial instance from that table.

Dynamic programming is comparatively slow but can solve many problems that greedy algorithm cannot solve.
It yields an optimal solution.

It does not have greedy-


choice property
Dynamic programming – Knapsack problem

I want to pack high


valuables with short time

A thief robbing a store finds n items


Summary

In this Module we have learnt about:


• Non-Linear Data Structures
o Tree and Graph
• Various Algorithm Design Techniques
o Greedy Algorithm
o Divide and Conquer
o Dynamic Programming
Thank You

You might also like