Top Banner
Algorithm Strategies Presented By M.Talha MCS-13-08
34
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Comparitive Analysis of Algorithm strategies

Algorithm StrategiesPresented By MTalha

MCS-13-08

Algorithms Strategy in Assignment

Decrease and ConquerGreedy ApproachBacktrackingTransform and Conquer

Roadmap of Every Strategy

Definition

Types

Example

Advantages

Disadvantages

Algorithm

Working

Complexity

Decrease and Conquer

Definition

_Change an instance into one smaller instance of the problem

_ Solve the smaller instance

_ Convert the solution of the smaller instance into a solution for the larger

Instance

Decrease and Conquer

Decrease by a Constant

insertion sort

graph traversal algorithms (DFS and BFS)

topological sorting

Decrease by a Constant Factor

The Fake-coin problem

Variable-size decrease

Euclidrsquos algorithm

Decrease and Conquer

Advantages

Solve Smaller Instance

Take less time

Use for shortest path Disadvantages

Depends on efficiency of sorting

Breadth-first search Algorithm

Definition

Breadth-first search (BFS) is an algorithm for traversing or searching tree or graph data structures It starts at the tree root (or some arbitrary node of a graph sometimes referred to as a `search key[1]) and explores the neighbor nodes first before moving to the next level neighbors

Algorithm of BFS

Procedure bfs (v)

q = make queue ()

Enqueue (q v)

Mark v as visited

While q is not empty

v = dequeue (q)

Process v

for all unvisited vertices v adjacent to v

Mark v as visited

enqueue(qv)

Order in which the nodes are expanded

Complexity

Θ (V2) if represented by an adjacency table

Θ (|V| + |E|) if represented by adjacency lists

Greedy Approach

Greedy algorithms are simple and straightforward

They are shortsighted in their approach in the sense that they take decisions on the basis of information at hand without worrying about the effect these decisions may have in the future

They are easy to invent easy to implement and most of the time quite efficient

Many problems cannot be solved correctly by greedy approach

Greedy algorithms are used to solve optimization problems

Uses of Greedy Approach

Greedy algorithms are often used in ad hoc mobile networking to efficiently route packets with the fewest number of hops and the shortest delay possible

They are also used in machine learning

business intelligence (BI)

artificial intelligence (AI) and

programming

Making Change Algorithm

Huffman Encoding Algorithm

Greedy Approach

Advantages

Very Large number of Feasible Solution

Easy to Implement

Disadvantages

It is much Slower

Does not give optimum result for all problems

Huffman Encoding

Huffman code is a particular type of optimal prefix code that is commonly used for lossless data compression

The process of finding andor using such a code proceeds by means of Huffman codding

The output from Huffmans algorithm can be viewed as a variable-length code table for encoding a source symbol (such as a character in a file)

httpenwikipediaorgwikiHuffman_coding

Huffman Encoding Algorithm

procedure Huffman(f)

Input An array f[1 _ _ _ n] of frequencies

Output An encoding tree with n leaves

let H be a priority queue of integers ordered by f

for i = 1 to n insert(H i)

for k = n + 1 to 2n 1048576 1

i = deletemin(H) j = deletemin(H)

create a node numbered k with children i j

f[k] = f[i] + f[j]

insert(H k)

httpenwikipediaorgwikiHuffman_coding

Working of the Huffman Encoding Algorithm

httpenwikipediaorgwikiHuffman_coding

Complexity

we use a greedy O(nlog(n))

httpwwwsiggraphorgeducationmaterialsHyperGraphvideompegmpegfaqhuffman_tutorialhtml

Knapsack Problem

You have a knapsack that has capacity (weight) W You have several items 1 to n Each item Ij has a weight wj and a benefit bj You want to place a certain number of copies of

each item Ij in the knapsack so that The knapsack weight capacity in not exceeded the total benefits is maximal

httpenwikipediaorgwikiKnapsack_problemApplications

Knapsack Problem

httpenwikipediaorgwikiKnapsack_problemmediaFileKnapsacksvg

Knapsack Problem Variants

01 knapsack Problem The item cannot be divided

Fractional knapsack problem For instance items are liquid or powders

Solvable with a greedy algorithm

httpenwikipediaorgwikiKnapsack_problemApplications

Greedy Knapsack - Fractional

Much Easier For item Ij let ratioj=bjwj This gives you

the benefit per measure of weight Sort the items in descending order of rj If the next item cannot fit into the

knapsack break it and pick it partially just to fill the knapsack

httpenwikipediaorgwikiKnapsack_problemApplications

Greedy Knapsack Algorithm

P and W are arrays contain the profit and weight n objects ordered such that p[i]w[i]=gtp[i+1]w[1+i] that is in decreasing order m is the knapsack size and x is the solution vector

GreedyKnapsack(mn)

bull For(i=0iltni++)

bull X[i]=0

bull Int total=m

bull For (i=0 iltn i++)

bull If(w[i] lt = total)

bull X[i]= 1

bull Total= total + w[i]

bull Else

bull X[i] = total w[i]

bull end Greedyknapsack

httpjava90blogspotcom201202knapsack-problem-in-javahtml

Example - Greedy Knapsack

Item A B C D

Benefit 50 140 60 60

weight 5 20 10 12

Ratio = BW 10 7 6 5

bullExample Knapsack Capacity W = 30 and

httpwwwradfordedu~nokieclasses360greedyhtml

Solution

All of A all of B and ((30-25)10) of C (and none of D) Weight 5 + 20 + 10(510) = 30

Value 50 + 140 + 60(510) = 190 + 30 = 220

httpwwwradfordedu~nokieclasses360greedyhtml

Analysis

If the items are already sorted into descending order of piwi

Then the for-loop takes a time in O(n) Therefore the Total time including the sort is in O(n log

n)

httpjava90blogspotcom201202knapsack-problem-in-javahtml

Backtracking

Backtracking is a technique for systematically trying all paths through a state space

Backtracking search begins at the start state and pursues a path until it reaches either a goal or a ldquodead endrdquo

If it finds a goal it quits and returns the solution path

If it reaches a dead end it ldquobacktracksrdquo to the most recent node on the path

having unexamined siblings and continues down one of these branches

enwikipediaorgwikiBacktracking

Backtracking

Backtracking is an important tool for solving

constraint satisfaction problems such as

crosswords

verbal arithmetic

Sudoku

and many other puzzles

8 Queen Puzzle Algorithm is an example of Backtracking

enwikipediaorgwikiBacktracking

Backtracking

Advantages

Quick test

Pair Matching

Following real life Concept

Disadvantages

Not widely Implemented

Cannot Express Left Recursion

More Time and Complexity

httpwwwclcamacuk~mr10backtrkpdf

Eight queens puzzle

The eight queens puzzle is the problem of placing eight chess queens on an 8times8 chessboard so that no two queens threaten each other

Thus a solution requires that no two queens share the same row column or diagonal

httpenwikipediaorgwikiEight_queens_puzzle

Eight queens puzzle Algorithm

Function backtrack

Begin

SL = [Start] NSL = [Start] DE = [] CS = Start initialize

While NSL ne [ ] do while there are states to be tried

Begin

If CS = goal (or meets goal description) then return SL on success return list of states in path

If CS has no children (excluding nodes already on DE SL and NSL) then

Begin

While SL is not empty and CS = the first element of SL do

Eight queens puzzle Algorithm

Begin

Add CS to DE

Remove first element from SL

Remove first element from NSL

CS ~ first element of NSL

End

Add CS to SL

End

Eight queens puzzle Algorithm

Else begin

Place children of CS (except nodes already on DE SL or NSL) on NSL

CS = first element of NSL

Add CS to SL

End

End

Eight queens puzzle Algorithm

httpwwwcodeprojectcomArticles806248Eight-Queens-Puzzle-Tree-Algorithm

Complexity

The running time of your algorithm is at most N(Nminus1)(Nminus2)⋯(NminusK+1) ie N(NminusK) This is O(NK) ie exponential in K

httpwwwcheggcomhomework-helpquestions-and-answerspoor-man-s-n-queens-problemn-queens-arranged-n-x-n-chessboard-way-queen-checks-queen-queen-q1009394

  • Algorithm Strategies
  • Algorithms Strategy in Assignment
  • Roadmap of Every Strategy
  • Decrease and Conquer
  • Decrease and Conquer
  • Decrease and Conquer
  • Breadth-first search Algorithm
  • Algorithm of BFS
  • Order in which the nodes are expanded
  • Complexity
  • Greedy Approach
  • Uses of Greedy Approach
  • Greedy Approach (2)
  • Huffman Encoding
  • Huffman Encoding Algorithm
  • Working of the Huffman Encoding Algorithm
  • Complexity (2)
  • Knapsack Problem
  • Knapsack Problem
  • Knapsack Problem Variants
  • Greedy Knapsack - Fractional
  • Greedy Knapsack Algorithm
  • Example - Greedy Knapsack
  • Solution
  • Analysis
  • Backtracking
  • Backtracking (2)
  • Backtracking (3)
  • Eight queens puzzle
  • Eight queens puzzle Algorithm
  • Eight queens puzzle Algorithm (2)
  • Eight queens puzzle Algorithm (3)
  • Eight queens puzzle Algorithm
  • Complexity (3)
Page 2: Comparitive Analysis of Algorithm strategies

Algorithms Strategy in Assignment

Decrease and ConquerGreedy ApproachBacktrackingTransform and Conquer

Roadmap of Every Strategy

Definition

Types

Example

Advantages

Disadvantages

Algorithm

Working

Complexity

Decrease and Conquer

Definition

_Change an instance into one smaller instance of the problem

_ Solve the smaller instance

_ Convert the solution of the smaller instance into a solution for the larger

Instance

Decrease and Conquer

Decrease by a Constant

insertion sort

graph traversal algorithms (DFS and BFS)

topological sorting

Decrease by a Constant Factor

The Fake-coin problem

Variable-size decrease

Euclidrsquos algorithm

Decrease and Conquer

Advantages

Solve Smaller Instance

Take less time

Use for shortest path Disadvantages

Depends on efficiency of sorting

Breadth-first search Algorithm

Definition

Breadth-first search (BFS) is an algorithm for traversing or searching tree or graph data structures It starts at the tree root (or some arbitrary node of a graph sometimes referred to as a `search key[1]) and explores the neighbor nodes first before moving to the next level neighbors

Algorithm of BFS

Procedure bfs (v)

q = make queue ()

Enqueue (q v)

Mark v as visited

While q is not empty

v = dequeue (q)

Process v

for all unvisited vertices v adjacent to v

Mark v as visited

enqueue(qv)

Order in which the nodes are expanded

Complexity

Θ (V2) if represented by an adjacency table

Θ (|V| + |E|) if represented by adjacency lists

Greedy Approach

Greedy algorithms are simple and straightforward

They are shortsighted in their approach in the sense that they take decisions on the basis of information at hand without worrying about the effect these decisions may have in the future

They are easy to invent easy to implement and most of the time quite efficient

Many problems cannot be solved correctly by greedy approach

Greedy algorithms are used to solve optimization problems

Uses of Greedy Approach

Greedy algorithms are often used in ad hoc mobile networking to efficiently route packets with the fewest number of hops and the shortest delay possible

They are also used in machine learning

business intelligence (BI)

artificial intelligence (AI) and

programming

Making Change Algorithm

Huffman Encoding Algorithm

Greedy Approach

Advantages

Very Large number of Feasible Solution

Easy to Implement

Disadvantages

It is much Slower

Does not give optimum result for all problems

Huffman Encoding

Huffman code is a particular type of optimal prefix code that is commonly used for lossless data compression

The process of finding andor using such a code proceeds by means of Huffman codding

The output from Huffmans algorithm can be viewed as a variable-length code table for encoding a source symbol (such as a character in a file)

httpenwikipediaorgwikiHuffman_coding

Huffman Encoding Algorithm

procedure Huffman(f)

Input An array f[1 _ _ _ n] of frequencies

Output An encoding tree with n leaves

let H be a priority queue of integers ordered by f

for i = 1 to n insert(H i)

for k = n + 1 to 2n 1048576 1

i = deletemin(H) j = deletemin(H)

create a node numbered k with children i j

f[k] = f[i] + f[j]

insert(H k)

httpenwikipediaorgwikiHuffman_coding

Working of the Huffman Encoding Algorithm

httpenwikipediaorgwikiHuffman_coding

Complexity

we use a greedy O(nlog(n))

httpwwwsiggraphorgeducationmaterialsHyperGraphvideompegmpegfaqhuffman_tutorialhtml

Knapsack Problem

You have a knapsack that has capacity (weight) W You have several items 1 to n Each item Ij has a weight wj and a benefit bj You want to place a certain number of copies of

each item Ij in the knapsack so that The knapsack weight capacity in not exceeded the total benefits is maximal

httpenwikipediaorgwikiKnapsack_problemApplications

Knapsack Problem

httpenwikipediaorgwikiKnapsack_problemmediaFileKnapsacksvg

Knapsack Problem Variants

01 knapsack Problem The item cannot be divided

Fractional knapsack problem For instance items are liquid or powders

Solvable with a greedy algorithm

httpenwikipediaorgwikiKnapsack_problemApplications

Greedy Knapsack - Fractional

Much Easier For item Ij let ratioj=bjwj This gives you

the benefit per measure of weight Sort the items in descending order of rj If the next item cannot fit into the

knapsack break it and pick it partially just to fill the knapsack

httpenwikipediaorgwikiKnapsack_problemApplications

Greedy Knapsack Algorithm

P and W are arrays contain the profit and weight n objects ordered such that p[i]w[i]=gtp[i+1]w[1+i] that is in decreasing order m is the knapsack size and x is the solution vector

GreedyKnapsack(mn)

bull For(i=0iltni++)

bull X[i]=0

bull Int total=m

bull For (i=0 iltn i++)

bull If(w[i] lt = total)

bull X[i]= 1

bull Total= total + w[i]

bull Else

bull X[i] = total w[i]

bull end Greedyknapsack

httpjava90blogspotcom201202knapsack-problem-in-javahtml

Example - Greedy Knapsack

Item A B C D

Benefit 50 140 60 60

weight 5 20 10 12

Ratio = BW 10 7 6 5

bullExample Knapsack Capacity W = 30 and

httpwwwradfordedu~nokieclasses360greedyhtml

Solution

All of A all of B and ((30-25)10) of C (and none of D) Weight 5 + 20 + 10(510) = 30

Value 50 + 140 + 60(510) = 190 + 30 = 220

httpwwwradfordedu~nokieclasses360greedyhtml

Analysis

If the items are already sorted into descending order of piwi

Then the for-loop takes a time in O(n) Therefore the Total time including the sort is in O(n log

n)

httpjava90blogspotcom201202knapsack-problem-in-javahtml

Backtracking

Backtracking is a technique for systematically trying all paths through a state space

Backtracking search begins at the start state and pursues a path until it reaches either a goal or a ldquodead endrdquo

If it finds a goal it quits and returns the solution path

If it reaches a dead end it ldquobacktracksrdquo to the most recent node on the path

having unexamined siblings and continues down one of these branches

enwikipediaorgwikiBacktracking

Backtracking

Backtracking is an important tool for solving

constraint satisfaction problems such as

crosswords

verbal arithmetic

Sudoku

and many other puzzles

8 Queen Puzzle Algorithm is an example of Backtracking

enwikipediaorgwikiBacktracking

Backtracking

Advantages

Quick test

Pair Matching

Following real life Concept

Disadvantages

Not widely Implemented

Cannot Express Left Recursion

More Time and Complexity

httpwwwclcamacuk~mr10backtrkpdf

Eight queens puzzle

The eight queens puzzle is the problem of placing eight chess queens on an 8times8 chessboard so that no two queens threaten each other

Thus a solution requires that no two queens share the same row column or diagonal

httpenwikipediaorgwikiEight_queens_puzzle

Eight queens puzzle Algorithm

Function backtrack

Begin

SL = [Start] NSL = [Start] DE = [] CS = Start initialize

While NSL ne [ ] do while there are states to be tried

Begin

If CS = goal (or meets goal description) then return SL on success return list of states in path

If CS has no children (excluding nodes already on DE SL and NSL) then

Begin

While SL is not empty and CS = the first element of SL do

Eight queens puzzle Algorithm

Begin

Add CS to DE

Remove first element from SL

Remove first element from NSL

CS ~ first element of NSL

End

Add CS to SL

End

Eight queens puzzle Algorithm

Else begin

Place children of CS (except nodes already on DE SL or NSL) on NSL

CS = first element of NSL

Add CS to SL

End

End

Eight queens puzzle Algorithm

httpwwwcodeprojectcomArticles806248Eight-Queens-Puzzle-Tree-Algorithm

Complexity

The running time of your algorithm is at most N(Nminus1)(Nminus2)⋯(NminusK+1) ie N(NminusK) This is O(NK) ie exponential in K

httpwwwcheggcomhomework-helpquestions-and-answerspoor-man-s-n-queens-problemn-queens-arranged-n-x-n-chessboard-way-queen-checks-queen-queen-q1009394

  • Algorithm Strategies
  • Algorithms Strategy in Assignment
  • Roadmap of Every Strategy
  • Decrease and Conquer
  • Decrease and Conquer
  • Decrease and Conquer
  • Breadth-first search Algorithm
  • Algorithm of BFS
  • Order in which the nodes are expanded
  • Complexity
  • Greedy Approach
  • Uses of Greedy Approach
  • Greedy Approach (2)
  • Huffman Encoding
  • Huffman Encoding Algorithm
  • Working of the Huffman Encoding Algorithm
  • Complexity (2)
  • Knapsack Problem
  • Knapsack Problem
  • Knapsack Problem Variants
  • Greedy Knapsack - Fractional
  • Greedy Knapsack Algorithm
  • Example - Greedy Knapsack
  • Solution
  • Analysis
  • Backtracking
  • Backtracking (2)
  • Backtracking (3)
  • Eight queens puzzle
  • Eight queens puzzle Algorithm
  • Eight queens puzzle Algorithm (2)
  • Eight queens puzzle Algorithm (3)
  • Eight queens puzzle Algorithm
  • Complexity (3)
Page 3: Comparitive Analysis of Algorithm strategies

Roadmap of Every Strategy

Definition

Types

Example

Advantages

Disadvantages

Algorithm

Working

Complexity

Decrease and Conquer

Definition

_Change an instance into one smaller instance of the problem

_ Solve the smaller instance

_ Convert the solution of the smaller instance into a solution for the larger

Instance

Decrease and Conquer

Decrease by a Constant

insertion sort

graph traversal algorithms (DFS and BFS)

topological sorting

Decrease by a Constant Factor

The Fake-coin problem

Variable-size decrease

Euclidrsquos algorithm

Decrease and Conquer

Advantages

Solve Smaller Instance

Take less time

Use for shortest path Disadvantages

Depends on efficiency of sorting

Breadth-first search Algorithm

Definition

Breadth-first search (BFS) is an algorithm for traversing or searching tree or graph data structures It starts at the tree root (or some arbitrary node of a graph sometimes referred to as a `search key[1]) and explores the neighbor nodes first before moving to the next level neighbors

Algorithm of BFS

Procedure bfs (v)

q = make queue ()

Enqueue (q v)

Mark v as visited

While q is not empty

v = dequeue (q)

Process v

for all unvisited vertices v adjacent to v

Mark v as visited

enqueue(qv)

Order in which the nodes are expanded

Complexity

Θ (V2) if represented by an adjacency table

Θ (|V| + |E|) if represented by adjacency lists

Greedy Approach

Greedy algorithms are simple and straightforward

They are shortsighted in their approach in the sense that they take decisions on the basis of information at hand without worrying about the effect these decisions may have in the future

They are easy to invent easy to implement and most of the time quite efficient

Many problems cannot be solved correctly by greedy approach

Greedy algorithms are used to solve optimization problems

Uses of Greedy Approach

Greedy algorithms are often used in ad hoc mobile networking to efficiently route packets with the fewest number of hops and the shortest delay possible

They are also used in machine learning

business intelligence (BI)

artificial intelligence (AI) and

programming

Making Change Algorithm

Huffman Encoding Algorithm

Greedy Approach

Advantages

Very Large number of Feasible Solution

Easy to Implement

Disadvantages

It is much Slower

Does not give optimum result for all problems

Huffman Encoding

Huffman code is a particular type of optimal prefix code that is commonly used for lossless data compression

The process of finding andor using such a code proceeds by means of Huffman codding

The output from Huffmans algorithm can be viewed as a variable-length code table for encoding a source symbol (such as a character in a file)

httpenwikipediaorgwikiHuffman_coding

Huffman Encoding Algorithm

procedure Huffman(f)

Input An array f[1 _ _ _ n] of frequencies

Output An encoding tree with n leaves

let H be a priority queue of integers ordered by f

for i = 1 to n insert(H i)

for k = n + 1 to 2n 1048576 1

i = deletemin(H) j = deletemin(H)

create a node numbered k with children i j

f[k] = f[i] + f[j]

insert(H k)

httpenwikipediaorgwikiHuffman_coding

Working of the Huffman Encoding Algorithm

httpenwikipediaorgwikiHuffman_coding

Complexity

we use a greedy O(nlog(n))

httpwwwsiggraphorgeducationmaterialsHyperGraphvideompegmpegfaqhuffman_tutorialhtml

Knapsack Problem

You have a knapsack that has capacity (weight) W You have several items 1 to n Each item Ij has a weight wj and a benefit bj You want to place a certain number of copies of

each item Ij in the knapsack so that The knapsack weight capacity in not exceeded the total benefits is maximal

httpenwikipediaorgwikiKnapsack_problemApplications

Knapsack Problem

httpenwikipediaorgwikiKnapsack_problemmediaFileKnapsacksvg

Knapsack Problem Variants

01 knapsack Problem The item cannot be divided

Fractional knapsack problem For instance items are liquid or powders

Solvable with a greedy algorithm

httpenwikipediaorgwikiKnapsack_problemApplications

Greedy Knapsack - Fractional

Much Easier For item Ij let ratioj=bjwj This gives you

the benefit per measure of weight Sort the items in descending order of rj If the next item cannot fit into the

knapsack break it and pick it partially just to fill the knapsack

httpenwikipediaorgwikiKnapsack_problemApplications

Greedy Knapsack Algorithm

P and W are arrays contain the profit and weight n objects ordered such that p[i]w[i]=gtp[i+1]w[1+i] that is in decreasing order m is the knapsack size and x is the solution vector

GreedyKnapsack(mn)

bull For(i=0iltni++)

bull X[i]=0

bull Int total=m

bull For (i=0 iltn i++)

bull If(w[i] lt = total)

bull X[i]= 1

bull Total= total + w[i]

bull Else

bull X[i] = total w[i]

bull end Greedyknapsack

httpjava90blogspotcom201202knapsack-problem-in-javahtml

Example - Greedy Knapsack

Item A B C D

Benefit 50 140 60 60

weight 5 20 10 12

Ratio = BW 10 7 6 5

bullExample Knapsack Capacity W = 30 and

httpwwwradfordedu~nokieclasses360greedyhtml

Solution

All of A all of B and ((30-25)10) of C (and none of D) Weight 5 + 20 + 10(510) = 30

Value 50 + 140 + 60(510) = 190 + 30 = 220

httpwwwradfordedu~nokieclasses360greedyhtml

Analysis

If the items are already sorted into descending order of piwi

Then the for-loop takes a time in O(n) Therefore the Total time including the sort is in O(n log

n)

httpjava90blogspotcom201202knapsack-problem-in-javahtml

Backtracking

Backtracking is a technique for systematically trying all paths through a state space

Backtracking search begins at the start state and pursues a path until it reaches either a goal or a ldquodead endrdquo

If it finds a goal it quits and returns the solution path

If it reaches a dead end it ldquobacktracksrdquo to the most recent node on the path

having unexamined siblings and continues down one of these branches

enwikipediaorgwikiBacktracking

Backtracking

Backtracking is an important tool for solving

constraint satisfaction problems such as

crosswords

verbal arithmetic

Sudoku

and many other puzzles

8 Queen Puzzle Algorithm is an example of Backtracking

enwikipediaorgwikiBacktracking

Backtracking

Advantages

Quick test

Pair Matching

Following real life Concept

Disadvantages

Not widely Implemented

Cannot Express Left Recursion

More Time and Complexity

httpwwwclcamacuk~mr10backtrkpdf

Eight queens puzzle

The eight queens puzzle is the problem of placing eight chess queens on an 8times8 chessboard so that no two queens threaten each other

Thus a solution requires that no two queens share the same row column or diagonal

httpenwikipediaorgwikiEight_queens_puzzle

Eight queens puzzle Algorithm

Function backtrack

Begin

SL = [Start] NSL = [Start] DE = [] CS = Start initialize

While NSL ne [ ] do while there are states to be tried

Begin

If CS = goal (or meets goal description) then return SL on success return list of states in path

If CS has no children (excluding nodes already on DE SL and NSL) then

Begin

While SL is not empty and CS = the first element of SL do

Eight queens puzzle Algorithm

Begin

Add CS to DE

Remove first element from SL

Remove first element from NSL

CS ~ first element of NSL

End

Add CS to SL

End

Eight queens puzzle Algorithm

Else begin

Place children of CS (except nodes already on DE SL or NSL) on NSL

CS = first element of NSL

Add CS to SL

End

End

Eight queens puzzle Algorithm

httpwwwcodeprojectcomArticles806248Eight-Queens-Puzzle-Tree-Algorithm

Complexity

The running time of your algorithm is at most N(Nminus1)(Nminus2)⋯(NminusK+1) ie N(NminusK) This is O(NK) ie exponential in K

httpwwwcheggcomhomework-helpquestions-and-answerspoor-man-s-n-queens-problemn-queens-arranged-n-x-n-chessboard-way-queen-checks-queen-queen-q1009394

  • Algorithm Strategies
  • Algorithms Strategy in Assignment
  • Roadmap of Every Strategy
  • Decrease and Conquer
  • Decrease and Conquer
  • Decrease and Conquer
  • Breadth-first search Algorithm
  • Algorithm of BFS
  • Order in which the nodes are expanded
  • Complexity
  • Greedy Approach
  • Uses of Greedy Approach
  • Greedy Approach (2)
  • Huffman Encoding
  • Huffman Encoding Algorithm
  • Working of the Huffman Encoding Algorithm
  • Complexity (2)
  • Knapsack Problem
  • Knapsack Problem
  • Knapsack Problem Variants
  • Greedy Knapsack - Fractional
  • Greedy Knapsack Algorithm
  • Example - Greedy Knapsack
  • Solution
  • Analysis
  • Backtracking
  • Backtracking (2)
  • Backtracking (3)
  • Eight queens puzzle
  • Eight queens puzzle Algorithm
  • Eight queens puzzle Algorithm (2)
  • Eight queens puzzle Algorithm (3)
  • Eight queens puzzle Algorithm
  • Complexity (3)
Page 4: Comparitive Analysis of Algorithm strategies

Decrease and Conquer

Definition

_Change an instance into one smaller instance of the problem

_ Solve the smaller instance

_ Convert the solution of the smaller instance into a solution for the larger

Instance

Decrease and Conquer

Decrease by a Constant

insertion sort

graph traversal algorithms (DFS and BFS)

topological sorting

Decrease by a Constant Factor

The Fake-coin problem

Variable-size decrease

Euclidrsquos algorithm

Decrease and Conquer

Advantages

Solve Smaller Instance

Take less time

Use for shortest path Disadvantages

Depends on efficiency of sorting

Breadth-first search Algorithm

Definition

Breadth-first search (BFS) is an algorithm for traversing or searching tree or graph data structures It starts at the tree root (or some arbitrary node of a graph sometimes referred to as a `search key[1]) and explores the neighbor nodes first before moving to the next level neighbors

Algorithm of BFS

Procedure bfs (v)

q = make queue ()

Enqueue (q v)

Mark v as visited

While q is not empty

v = dequeue (q)

Process v

for all unvisited vertices v adjacent to v

Mark v as visited

enqueue(qv)

Order in which the nodes are expanded

Complexity

Θ (V2) if represented by an adjacency table

Θ (|V| + |E|) if represented by adjacency lists

Greedy Approach

Greedy algorithms are simple and straightforward

They are shortsighted in their approach in the sense that they take decisions on the basis of information at hand without worrying about the effect these decisions may have in the future

They are easy to invent easy to implement and most of the time quite efficient

Many problems cannot be solved correctly by greedy approach

Greedy algorithms are used to solve optimization problems

Uses of Greedy Approach

Greedy algorithms are often used in ad hoc mobile networking to efficiently route packets with the fewest number of hops and the shortest delay possible

They are also used in machine learning

business intelligence (BI)

artificial intelligence (AI) and

programming

Making Change Algorithm

Huffman Encoding Algorithm

Greedy Approach

Advantages

Very Large number of Feasible Solution

Easy to Implement

Disadvantages

It is much Slower

Does not give optimum result for all problems

Huffman Encoding

Huffman code is a particular type of optimal prefix code that is commonly used for lossless data compression

The process of finding andor using such a code proceeds by means of Huffman codding

The output from Huffmans algorithm can be viewed as a variable-length code table for encoding a source symbol (such as a character in a file)

httpenwikipediaorgwikiHuffman_coding

Huffman Encoding Algorithm

procedure Huffman(f)

Input An array f[1 _ _ _ n] of frequencies

Output An encoding tree with n leaves

let H be a priority queue of integers ordered by f

for i = 1 to n insert(H i)

for k = n + 1 to 2n 1048576 1

i = deletemin(H) j = deletemin(H)

create a node numbered k with children i j

f[k] = f[i] + f[j]

insert(H k)

httpenwikipediaorgwikiHuffman_coding

Working of the Huffman Encoding Algorithm

httpenwikipediaorgwikiHuffman_coding

Complexity

we use a greedy O(nlog(n))

httpwwwsiggraphorgeducationmaterialsHyperGraphvideompegmpegfaqhuffman_tutorialhtml

Knapsack Problem

You have a knapsack that has capacity (weight) W You have several items 1 to n Each item Ij has a weight wj and a benefit bj You want to place a certain number of copies of

each item Ij in the knapsack so that The knapsack weight capacity in not exceeded the total benefits is maximal

httpenwikipediaorgwikiKnapsack_problemApplications

Knapsack Problem

httpenwikipediaorgwikiKnapsack_problemmediaFileKnapsacksvg

Knapsack Problem Variants

01 knapsack Problem The item cannot be divided

Fractional knapsack problem For instance items are liquid or powders

Solvable with a greedy algorithm

httpenwikipediaorgwikiKnapsack_problemApplications

Greedy Knapsack - Fractional

Much Easier For item Ij let ratioj=bjwj This gives you

the benefit per measure of weight Sort the items in descending order of rj If the next item cannot fit into the

knapsack break it and pick it partially just to fill the knapsack

httpenwikipediaorgwikiKnapsack_problemApplications

Greedy Knapsack Algorithm

P and W are arrays contain the profit and weight n objects ordered such that p[i]w[i]=gtp[i+1]w[1+i] that is in decreasing order m is the knapsack size and x is the solution vector

GreedyKnapsack(mn)

bull For(i=0iltni++)

bull X[i]=0

bull Int total=m

bull For (i=0 iltn i++)

bull If(w[i] lt = total)

bull X[i]= 1

bull Total= total + w[i]

bull Else

bull X[i] = total w[i]

bull end Greedyknapsack

httpjava90blogspotcom201202knapsack-problem-in-javahtml

Example - Greedy Knapsack

Item A B C D

Benefit 50 140 60 60

weight 5 20 10 12

Ratio = BW 10 7 6 5

bullExample Knapsack Capacity W = 30 and

httpwwwradfordedu~nokieclasses360greedyhtml

Solution

All of A all of B and ((30-25)10) of C (and none of D) Weight 5 + 20 + 10(510) = 30

Value 50 + 140 + 60(510) = 190 + 30 = 220

httpwwwradfordedu~nokieclasses360greedyhtml

Analysis

If the items are already sorted into descending order of piwi

Then the for-loop takes a time in O(n) Therefore the Total time including the sort is in O(n log

n)

httpjava90blogspotcom201202knapsack-problem-in-javahtml

Backtracking

Backtracking is a technique for systematically trying all paths through a state space

Backtracking search begins at the start state and pursues a path until it reaches either a goal or a ldquodead endrdquo

If it finds a goal it quits and returns the solution path

If it reaches a dead end it ldquobacktracksrdquo to the most recent node on the path

having unexamined siblings and continues down one of these branches

enwikipediaorgwikiBacktracking

Backtracking

Backtracking is an important tool for solving

constraint satisfaction problems such as

crosswords

verbal arithmetic

Sudoku

and many other puzzles

8 Queen Puzzle Algorithm is an example of Backtracking

enwikipediaorgwikiBacktracking

Backtracking

Advantages

Quick test

Pair Matching

Following real life Concept

Disadvantages

Not widely Implemented

Cannot Express Left Recursion

More Time and Complexity

httpwwwclcamacuk~mr10backtrkpdf

Eight queens puzzle

The eight queens puzzle is the problem of placing eight chess queens on an 8times8 chessboard so that no two queens threaten each other

Thus a solution requires that no two queens share the same row column or diagonal

httpenwikipediaorgwikiEight_queens_puzzle

Eight queens puzzle Algorithm

Function backtrack

Begin

SL = [Start] NSL = [Start] DE = [] CS = Start initialize

While NSL ne [ ] do while there are states to be tried

Begin

If CS = goal (or meets goal description) then return SL on success return list of states in path

If CS has no children (excluding nodes already on DE SL and NSL) then

Begin

While SL is not empty and CS = the first element of SL do

Eight queens puzzle Algorithm

Begin

Add CS to DE

Remove first element from SL

Remove first element from NSL

CS ~ first element of NSL

End

Add CS to SL

End

Eight queens puzzle Algorithm

Else begin

Place children of CS (except nodes already on DE SL or NSL) on NSL

CS = first element of NSL

Add CS to SL

End

End

Eight queens puzzle Algorithm

httpwwwcodeprojectcomArticles806248Eight-Queens-Puzzle-Tree-Algorithm

Complexity

The running time of your algorithm is at most N(Nminus1)(Nminus2)⋯(NminusK+1) ie N(NminusK) This is O(NK) ie exponential in K

httpwwwcheggcomhomework-helpquestions-and-answerspoor-man-s-n-queens-problemn-queens-arranged-n-x-n-chessboard-way-queen-checks-queen-queen-q1009394

  • Algorithm Strategies
  • Algorithms Strategy in Assignment
  • Roadmap of Every Strategy
  • Decrease and Conquer
  • Decrease and Conquer
  • Decrease and Conquer
  • Breadth-first search Algorithm
  • Algorithm of BFS
  • Order in which the nodes are expanded
  • Complexity
  • Greedy Approach
  • Uses of Greedy Approach
  • Greedy Approach (2)
  • Huffman Encoding
  • Huffman Encoding Algorithm
  • Working of the Huffman Encoding Algorithm
  • Complexity (2)
  • Knapsack Problem
  • Knapsack Problem
  • Knapsack Problem Variants
  • Greedy Knapsack - Fractional
  • Greedy Knapsack Algorithm
  • Example - Greedy Knapsack
  • Solution
  • Analysis
  • Backtracking
  • Backtracking (2)
  • Backtracking (3)
  • Eight queens puzzle
  • Eight queens puzzle Algorithm
  • Eight queens puzzle Algorithm (2)
  • Eight queens puzzle Algorithm (3)
  • Eight queens puzzle Algorithm
  • Complexity (3)
Page 5: Comparitive Analysis of Algorithm strategies

Decrease and Conquer

Decrease by a Constant

insertion sort

graph traversal algorithms (DFS and BFS)

topological sorting

Decrease by a Constant Factor

The Fake-coin problem

Variable-size decrease

Euclidrsquos algorithm

Decrease and Conquer

Advantages

Solve Smaller Instance

Take less time

Use for shortest path Disadvantages

Depends on efficiency of sorting

Breadth-first search Algorithm

Definition

Breadth-first search (BFS) is an algorithm for traversing or searching tree or graph data structures It starts at the tree root (or some arbitrary node of a graph sometimes referred to as a `search key[1]) and explores the neighbor nodes first before moving to the next level neighbors

Algorithm of BFS

Procedure bfs (v)

q = make queue ()

Enqueue (q v)

Mark v as visited

While q is not empty

v = dequeue (q)

Process v

for all unvisited vertices v adjacent to v

Mark v as visited

enqueue(qv)

Order in which the nodes are expanded

Complexity

Θ (V2) if represented by an adjacency table

Θ (|V| + |E|) if represented by adjacency lists

Greedy Approach

Greedy algorithms are simple and straightforward

They are shortsighted in their approach in the sense that they take decisions on the basis of information at hand without worrying about the effect these decisions may have in the future

They are easy to invent easy to implement and most of the time quite efficient

Many problems cannot be solved correctly by greedy approach

Greedy algorithms are used to solve optimization problems

Uses of Greedy Approach

Greedy algorithms are often used in ad hoc mobile networking to efficiently route packets with the fewest number of hops and the shortest delay possible

They are also used in machine learning

business intelligence (BI)

artificial intelligence (AI) and

programming

Making Change Algorithm

Huffman Encoding Algorithm

Greedy Approach

Advantages

Very Large number of Feasible Solution

Easy to Implement

Disadvantages

It is much Slower

Does not give optimum result for all problems

Huffman Encoding

Huffman code is a particular type of optimal prefix code that is commonly used for lossless data compression

The process of finding andor using such a code proceeds by means of Huffman codding

The output from Huffmans algorithm can be viewed as a variable-length code table for encoding a source symbol (such as a character in a file)

httpenwikipediaorgwikiHuffman_coding

Huffman Encoding Algorithm

procedure Huffman(f)

Input An array f[1 _ _ _ n] of frequencies

Output An encoding tree with n leaves

let H be a priority queue of integers ordered by f

for i = 1 to n insert(H i)

for k = n + 1 to 2n 1048576 1

i = deletemin(H) j = deletemin(H)

create a node numbered k with children i j

f[k] = f[i] + f[j]

insert(H k)

httpenwikipediaorgwikiHuffman_coding

Working of the Huffman Encoding Algorithm

httpenwikipediaorgwikiHuffman_coding

Complexity

we use a greedy O(nlog(n))

httpwwwsiggraphorgeducationmaterialsHyperGraphvideompegmpegfaqhuffman_tutorialhtml

Knapsack Problem

You have a knapsack that has capacity (weight) W You have several items 1 to n Each item Ij has a weight wj and a benefit bj You want to place a certain number of copies of

each item Ij in the knapsack so that The knapsack weight capacity in not exceeded the total benefits is maximal

httpenwikipediaorgwikiKnapsack_problemApplications

Knapsack Problem

httpenwikipediaorgwikiKnapsack_problemmediaFileKnapsacksvg

Knapsack Problem Variants

01 knapsack Problem The item cannot be divided

Fractional knapsack problem For instance items are liquid or powders

Solvable with a greedy algorithm

httpenwikipediaorgwikiKnapsack_problemApplications

Greedy Knapsack - Fractional

Much Easier For item Ij let ratioj=bjwj This gives you

the benefit per measure of weight Sort the items in descending order of rj If the next item cannot fit into the

knapsack break it and pick it partially just to fill the knapsack

httpenwikipediaorgwikiKnapsack_problemApplications

Greedy Knapsack Algorithm

P and W are arrays contain the profit and weight n objects ordered such that p[i]w[i]=gtp[i+1]w[1+i] that is in decreasing order m is the knapsack size and x is the solution vector

GreedyKnapsack(mn)

bull For(i=0iltni++)

bull X[i]=0

bull Int total=m

bull For (i=0 iltn i++)

bull If(w[i] lt = total)

bull X[i]= 1

bull Total= total + w[i]

bull Else

bull X[i] = total w[i]

bull end Greedyknapsack

httpjava90blogspotcom201202knapsack-problem-in-javahtml

Example - Greedy Knapsack

Item A B C D

Benefit 50 140 60 60

weight 5 20 10 12

Ratio = BW 10 7 6 5

bullExample Knapsack Capacity W = 30 and

httpwwwradfordedu~nokieclasses360greedyhtml

Solution

All of A all of B and ((30-25)10) of C (and none of D) Weight 5 + 20 + 10(510) = 30

Value 50 + 140 + 60(510) = 190 + 30 = 220

httpwwwradfordedu~nokieclasses360greedyhtml

Analysis

If the items are already sorted into descending order of piwi

Then the for-loop takes a time in O(n) Therefore the Total time including the sort is in O(n log

n)

httpjava90blogspotcom201202knapsack-problem-in-javahtml

Backtracking

Backtracking is a technique for systematically trying all paths through a state space

Backtracking search begins at the start state and pursues a path until it reaches either a goal or a ldquodead endrdquo

If it finds a goal it quits and returns the solution path

If it reaches a dead end it ldquobacktracksrdquo to the most recent node on the path

having unexamined siblings and continues down one of these branches

enwikipediaorgwikiBacktracking

Backtracking

Backtracking is an important tool for solving

constraint satisfaction problems such as

crosswords

verbal arithmetic

Sudoku

and many other puzzles

8 Queen Puzzle Algorithm is an example of Backtracking

enwikipediaorgwikiBacktracking

Backtracking

Advantages

Quick test

Pair Matching

Following real life Concept

Disadvantages

Not widely Implemented

Cannot Express Left Recursion

More Time and Complexity

httpwwwclcamacuk~mr10backtrkpdf

Eight queens puzzle

The eight queens puzzle is the problem of placing eight chess queens on an 8times8 chessboard so that no two queens threaten each other

Thus a solution requires that no two queens share the same row column or diagonal

httpenwikipediaorgwikiEight_queens_puzzle

Eight queens puzzle Algorithm

Function backtrack

Begin

SL = [Start] NSL = [Start] DE = [] CS = Start initialize

While NSL ne [ ] do while there are states to be tried

Begin

If CS = goal (or meets goal description) then return SL on success return list of states in path

If CS has no children (excluding nodes already on DE SL and NSL) then

Begin

While SL is not empty and CS = the first element of SL do

Eight queens puzzle Algorithm

Begin

Add CS to DE

Remove first element from SL

Remove first element from NSL

CS ~ first element of NSL

End

Add CS to SL

End

Eight queens puzzle Algorithm

Else begin

Place children of CS (except nodes already on DE SL or NSL) on NSL

CS = first element of NSL

Add CS to SL

End

End

Eight queens puzzle Algorithm

httpwwwcodeprojectcomArticles806248Eight-Queens-Puzzle-Tree-Algorithm

Complexity

The running time of your algorithm is at most N(Nminus1)(Nminus2)⋯(NminusK+1) ie N(NminusK) This is O(NK) ie exponential in K

httpwwwcheggcomhomework-helpquestions-and-answerspoor-man-s-n-queens-problemn-queens-arranged-n-x-n-chessboard-way-queen-checks-queen-queen-q1009394

  • Algorithm Strategies
  • Algorithms Strategy in Assignment
  • Roadmap of Every Strategy
  • Decrease and Conquer
  • Decrease and Conquer
  • Decrease and Conquer
  • Breadth-first search Algorithm
  • Algorithm of BFS
  • Order in which the nodes are expanded
  • Complexity
  • Greedy Approach
  • Uses of Greedy Approach
  • Greedy Approach (2)
  • Huffman Encoding
  • Huffman Encoding Algorithm
  • Working of the Huffman Encoding Algorithm
  • Complexity (2)
  • Knapsack Problem
  • Knapsack Problem
  • Knapsack Problem Variants
  • Greedy Knapsack - Fractional
  • Greedy Knapsack Algorithm
  • Example - Greedy Knapsack
  • Solution
  • Analysis
  • Backtracking
  • Backtracking (2)
  • Backtracking (3)
  • Eight queens puzzle
  • Eight queens puzzle Algorithm
  • Eight queens puzzle Algorithm (2)
  • Eight queens puzzle Algorithm (3)
  • Eight queens puzzle Algorithm
  • Complexity (3)
Page 6: Comparitive Analysis of Algorithm strategies

Decrease and Conquer

Advantages

Solve Smaller Instance

Take less time

Use for shortest path Disadvantages

Depends on efficiency of sorting

Breadth-first search Algorithm

Definition

Breadth-first search (BFS) is an algorithm for traversing or searching tree or graph data structures It starts at the tree root (or some arbitrary node of a graph sometimes referred to as a `search key[1]) and explores the neighbor nodes first before moving to the next level neighbors

Algorithm of BFS

Procedure bfs (v)

q = make queue ()

Enqueue (q v)

Mark v as visited

While q is not empty

v = dequeue (q)

Process v

for all unvisited vertices v adjacent to v

Mark v as visited

enqueue(qv)

Order in which the nodes are expanded

Complexity

Θ (V2) if represented by an adjacency table

Θ (|V| + |E|) if represented by adjacency lists

Greedy Approach

Greedy algorithms are simple and straightforward

They are shortsighted in their approach in the sense that they take decisions on the basis of information at hand without worrying about the effect these decisions may have in the future

They are easy to invent easy to implement and most of the time quite efficient

Many problems cannot be solved correctly by greedy approach

Greedy algorithms are used to solve optimization problems

Uses of Greedy Approach

Greedy algorithms are often used in ad hoc mobile networking to efficiently route packets with the fewest number of hops and the shortest delay possible

They are also used in machine learning

business intelligence (BI)

artificial intelligence (AI) and

programming

Making Change Algorithm

Huffman Encoding Algorithm

Greedy Approach

Advantages

Very Large number of Feasible Solution

Easy to Implement

Disadvantages

It is much Slower

Does not give optimum result for all problems

Huffman Encoding

Huffman code is a particular type of optimal prefix code that is commonly used for lossless data compression

The process of finding andor using such a code proceeds by means of Huffman codding

The output from Huffmans algorithm can be viewed as a variable-length code table for encoding a source symbol (such as a character in a file)

httpenwikipediaorgwikiHuffman_coding

Huffman Encoding Algorithm

procedure Huffman(f)

Input An array f[1 _ _ _ n] of frequencies

Output An encoding tree with n leaves

let H be a priority queue of integers ordered by f

for i = 1 to n insert(H i)

for k = n + 1 to 2n 1048576 1

i = deletemin(H) j = deletemin(H)

create a node numbered k with children i j

f[k] = f[i] + f[j]

insert(H k)

httpenwikipediaorgwikiHuffman_coding

Working of the Huffman Encoding Algorithm

httpenwikipediaorgwikiHuffman_coding

Complexity

we use a greedy O(nlog(n))

httpwwwsiggraphorgeducationmaterialsHyperGraphvideompegmpegfaqhuffman_tutorialhtml

Knapsack Problem

You have a knapsack that has capacity (weight) W You have several items 1 to n Each item Ij has a weight wj and a benefit bj You want to place a certain number of copies of

each item Ij in the knapsack so that The knapsack weight capacity in not exceeded the total benefits is maximal

httpenwikipediaorgwikiKnapsack_problemApplications

Knapsack Problem

httpenwikipediaorgwikiKnapsack_problemmediaFileKnapsacksvg

Knapsack Problem Variants

01 knapsack Problem The item cannot be divided

Fractional knapsack problem For instance items are liquid or powders

Solvable with a greedy algorithm

httpenwikipediaorgwikiKnapsack_problemApplications

Greedy Knapsack - Fractional

Much Easier For item Ij let ratioj=bjwj This gives you

the benefit per measure of weight Sort the items in descending order of rj If the next item cannot fit into the

knapsack break it and pick it partially just to fill the knapsack

httpenwikipediaorgwikiKnapsack_problemApplications

Greedy Knapsack Algorithm

P and W are arrays contain the profit and weight n objects ordered such that p[i]w[i]=gtp[i+1]w[1+i] that is in decreasing order m is the knapsack size and x is the solution vector

GreedyKnapsack(mn)

bull For(i=0iltni++)

bull X[i]=0

bull Int total=m

bull For (i=0 iltn i++)

bull If(w[i] lt = total)

bull X[i]= 1

bull Total= total + w[i]

bull Else

bull X[i] = total w[i]

bull end Greedyknapsack

httpjava90blogspotcom201202knapsack-problem-in-javahtml

Example - Greedy Knapsack

Item A B C D

Benefit 50 140 60 60

weight 5 20 10 12

Ratio = BW 10 7 6 5

bullExample Knapsack Capacity W = 30 and

httpwwwradfordedu~nokieclasses360greedyhtml

Solution

All of A all of B and ((30-25)10) of C (and none of D) Weight 5 + 20 + 10(510) = 30

Value 50 + 140 + 60(510) = 190 + 30 = 220

httpwwwradfordedu~nokieclasses360greedyhtml

Analysis

If the items are already sorted into descending order of piwi

Then the for-loop takes a time in O(n) Therefore the Total time including the sort is in O(n log

n)

httpjava90blogspotcom201202knapsack-problem-in-javahtml

Backtracking

Backtracking is a technique for systematically trying all paths through a state space

Backtracking search begins at the start state and pursues a path until it reaches either a goal or a ldquodead endrdquo

If it finds a goal it quits and returns the solution path

If it reaches a dead end it ldquobacktracksrdquo to the most recent node on the path

having unexamined siblings and continues down one of these branches

enwikipediaorgwikiBacktracking

Backtracking

Backtracking is an important tool for solving

constraint satisfaction problems such as

crosswords

verbal arithmetic

Sudoku

and many other puzzles

8 Queen Puzzle Algorithm is an example of Backtracking

enwikipediaorgwikiBacktracking

Backtracking

Advantages

Quick test

Pair Matching

Following real life Concept

Disadvantages

Not widely Implemented

Cannot Express Left Recursion

More Time and Complexity

httpwwwclcamacuk~mr10backtrkpdf

Eight queens puzzle

The eight queens puzzle is the problem of placing eight chess queens on an 8times8 chessboard so that no two queens threaten each other

Thus a solution requires that no two queens share the same row column or diagonal

httpenwikipediaorgwikiEight_queens_puzzle

Eight queens puzzle Algorithm

Function backtrack

Begin

SL = [Start] NSL = [Start] DE = [] CS = Start initialize

While NSL ne [ ] do while there are states to be tried

Begin

If CS = goal (or meets goal description) then return SL on success return list of states in path

If CS has no children (excluding nodes already on DE SL and NSL) then

Begin

While SL is not empty and CS = the first element of SL do

Eight queens puzzle Algorithm

Begin

Add CS to DE

Remove first element from SL

Remove first element from NSL

CS ~ first element of NSL

End

Add CS to SL

End

Eight queens puzzle Algorithm

Else begin

Place children of CS (except nodes already on DE SL or NSL) on NSL

CS = first element of NSL

Add CS to SL

End

End

Eight queens puzzle Algorithm

httpwwwcodeprojectcomArticles806248Eight-Queens-Puzzle-Tree-Algorithm

Complexity

The running time of your algorithm is at most N(Nminus1)(Nminus2)⋯(NminusK+1) ie N(NminusK) This is O(NK) ie exponential in K

httpwwwcheggcomhomework-helpquestions-and-answerspoor-man-s-n-queens-problemn-queens-arranged-n-x-n-chessboard-way-queen-checks-queen-queen-q1009394

  • Algorithm Strategies
  • Algorithms Strategy in Assignment
  • Roadmap of Every Strategy
  • Decrease and Conquer
  • Decrease and Conquer
  • Decrease and Conquer
  • Breadth-first search Algorithm
  • Algorithm of BFS
  • Order in which the nodes are expanded
  • Complexity
  • Greedy Approach
  • Uses of Greedy Approach
  • Greedy Approach (2)
  • Huffman Encoding
  • Huffman Encoding Algorithm
  • Working of the Huffman Encoding Algorithm
  • Complexity (2)
  • Knapsack Problem
  • Knapsack Problem
  • Knapsack Problem Variants
  • Greedy Knapsack - Fractional
  • Greedy Knapsack Algorithm
  • Example - Greedy Knapsack
  • Solution
  • Analysis
  • Backtracking
  • Backtracking (2)
  • Backtracking (3)
  • Eight queens puzzle
  • Eight queens puzzle Algorithm
  • Eight queens puzzle Algorithm (2)
  • Eight queens puzzle Algorithm (3)
  • Eight queens puzzle Algorithm
  • Complexity (3)
Page 7: Comparitive Analysis of Algorithm strategies

Breadth-first search Algorithm

Definition

Breadth-first search (BFS) is an algorithm for traversing or searching tree or graph data structures It starts at the tree root (or some arbitrary node of a graph sometimes referred to as a `search key[1]) and explores the neighbor nodes first before moving to the next level neighbors

Algorithm of BFS

Procedure bfs (v)

q = make queue ()

Enqueue (q v)

Mark v as visited

While q is not empty

v = dequeue (q)

Process v

for all unvisited vertices v adjacent to v

Mark v as visited

enqueue(qv)

Order in which the nodes are expanded

Complexity

Θ (V2) if represented by an adjacency table

Θ (|V| + |E|) if represented by adjacency lists

Greedy Approach

Greedy algorithms are simple and straightforward

They are shortsighted in their approach in the sense that they take decisions on the basis of information at hand without worrying about the effect these decisions may have in the future

They are easy to invent easy to implement and most of the time quite efficient

Many problems cannot be solved correctly by greedy approach

Greedy algorithms are used to solve optimization problems

Uses of Greedy Approach

Greedy algorithms are often used in ad hoc mobile networking to efficiently route packets with the fewest number of hops and the shortest delay possible

They are also used in machine learning

business intelligence (BI)

artificial intelligence (AI) and

programming

Making Change Algorithm

Huffman Encoding Algorithm

Greedy Approach

Advantages

Very Large number of Feasible Solution

Easy to Implement

Disadvantages

It is much Slower

Does not give optimum result for all problems

Huffman Encoding

Huffman code is a particular type of optimal prefix code that is commonly used for lossless data compression

The process of finding andor using such a code proceeds by means of Huffman codding

The output from Huffmans algorithm can be viewed as a variable-length code table for encoding a source symbol (such as a character in a file)

httpenwikipediaorgwikiHuffman_coding

Huffman Encoding Algorithm

procedure Huffman(f)

Input An array f[1 _ _ _ n] of frequencies

Output An encoding tree with n leaves

let H be a priority queue of integers ordered by f

for i = 1 to n insert(H i)

for k = n + 1 to 2n 1048576 1

i = deletemin(H) j = deletemin(H)

create a node numbered k with children i j

f[k] = f[i] + f[j]

insert(H k)

httpenwikipediaorgwikiHuffman_coding

Working of the Huffman Encoding Algorithm

httpenwikipediaorgwikiHuffman_coding

Complexity

we use a greedy O(nlog(n))

httpwwwsiggraphorgeducationmaterialsHyperGraphvideompegmpegfaqhuffman_tutorialhtml

Knapsack Problem

You have a knapsack that has capacity (weight) W You have several items 1 to n Each item Ij has a weight wj and a benefit bj You want to place a certain number of copies of

each item Ij in the knapsack so that The knapsack weight capacity in not exceeded the total benefits is maximal

httpenwikipediaorgwikiKnapsack_problemApplications

Knapsack Problem

httpenwikipediaorgwikiKnapsack_problemmediaFileKnapsacksvg

Knapsack Problem Variants

01 knapsack Problem The item cannot be divided

Fractional knapsack problem For instance items are liquid or powders

Solvable with a greedy algorithm

httpenwikipediaorgwikiKnapsack_problemApplications

Greedy Knapsack - Fractional

Much Easier For item Ij let ratioj=bjwj This gives you

the benefit per measure of weight Sort the items in descending order of rj If the next item cannot fit into the

knapsack break it and pick it partially just to fill the knapsack

httpenwikipediaorgwikiKnapsack_problemApplications

Greedy Knapsack Algorithm

P and W are arrays contain the profit and weight n objects ordered such that p[i]w[i]=gtp[i+1]w[1+i] that is in decreasing order m is the knapsack size and x is the solution vector

GreedyKnapsack(mn)

bull For(i=0iltni++)

bull X[i]=0

bull Int total=m

bull For (i=0 iltn i++)

bull If(w[i] lt = total)

bull X[i]= 1

bull Total= total + w[i]

bull Else

bull X[i] = total w[i]

bull end Greedyknapsack

httpjava90blogspotcom201202knapsack-problem-in-javahtml

Example - Greedy Knapsack

Item A B C D

Benefit 50 140 60 60

weight 5 20 10 12

Ratio = BW 10 7 6 5

bullExample Knapsack Capacity W = 30 and

httpwwwradfordedu~nokieclasses360greedyhtml

Solution

All of A all of B and ((30-25)10) of C (and none of D) Weight 5 + 20 + 10(510) = 30

Value 50 + 140 + 60(510) = 190 + 30 = 220

httpwwwradfordedu~nokieclasses360greedyhtml

Analysis

If the items are already sorted into descending order of piwi

Then the for-loop takes a time in O(n) Therefore the Total time including the sort is in O(n log

n)

httpjava90blogspotcom201202knapsack-problem-in-javahtml

Backtracking

Backtracking is a technique for systematically trying all paths through a state space

Backtracking search begins at the start state and pursues a path until it reaches either a goal or a ldquodead endrdquo

If it finds a goal it quits and returns the solution path

If it reaches a dead end it ldquobacktracksrdquo to the most recent node on the path

having unexamined siblings and continues down one of these branches

enwikipediaorgwikiBacktracking

Backtracking

Backtracking is an important tool for solving

constraint satisfaction problems such as

crosswords

verbal arithmetic

Sudoku

and many other puzzles

8 Queen Puzzle Algorithm is an example of Backtracking

enwikipediaorgwikiBacktracking

Backtracking

Advantages

Quick test

Pair Matching

Following real life Concept

Disadvantages

Not widely Implemented

Cannot Express Left Recursion

More Time and Complexity

httpwwwclcamacuk~mr10backtrkpdf

Eight queens puzzle

The eight queens puzzle is the problem of placing eight chess queens on an 8times8 chessboard so that no two queens threaten each other

Thus a solution requires that no two queens share the same row column or diagonal

httpenwikipediaorgwikiEight_queens_puzzle

Eight queens puzzle Algorithm

Function backtrack

Begin

SL = [Start] NSL = [Start] DE = [] CS = Start initialize

While NSL ne [ ] do while there are states to be tried

Begin

If CS = goal (or meets goal description) then return SL on success return list of states in path

If CS has no children (excluding nodes already on DE SL and NSL) then

Begin

While SL is not empty and CS = the first element of SL do

Eight queens puzzle Algorithm

Begin

Add CS to DE

Remove first element from SL

Remove first element from NSL

CS ~ first element of NSL

End

Add CS to SL

End

Eight queens puzzle Algorithm

Else begin

Place children of CS (except nodes already on DE SL or NSL) on NSL

CS = first element of NSL

Add CS to SL

End

End

Eight queens puzzle Algorithm

httpwwwcodeprojectcomArticles806248Eight-Queens-Puzzle-Tree-Algorithm

Complexity

The running time of your algorithm is at most N(Nminus1)(Nminus2)⋯(NminusK+1) ie N(NminusK) This is O(NK) ie exponential in K

httpwwwcheggcomhomework-helpquestions-and-answerspoor-man-s-n-queens-problemn-queens-arranged-n-x-n-chessboard-way-queen-checks-queen-queen-q1009394

  • Algorithm Strategies
  • Algorithms Strategy in Assignment
  • Roadmap of Every Strategy
  • Decrease and Conquer
  • Decrease and Conquer
  • Decrease and Conquer
  • Breadth-first search Algorithm
  • Algorithm of BFS
  • Order in which the nodes are expanded
  • Complexity
  • Greedy Approach
  • Uses of Greedy Approach
  • Greedy Approach (2)
  • Huffman Encoding
  • Huffman Encoding Algorithm
  • Working of the Huffman Encoding Algorithm
  • Complexity (2)
  • Knapsack Problem
  • Knapsack Problem
  • Knapsack Problem Variants
  • Greedy Knapsack - Fractional
  • Greedy Knapsack Algorithm
  • Example - Greedy Knapsack
  • Solution
  • Analysis
  • Backtracking
  • Backtracking (2)
  • Backtracking (3)
  • Eight queens puzzle
  • Eight queens puzzle Algorithm
  • Eight queens puzzle Algorithm (2)
  • Eight queens puzzle Algorithm (3)
  • Eight queens puzzle Algorithm
  • Complexity (3)
Page 8: Comparitive Analysis of Algorithm strategies

Algorithm of BFS

Procedure bfs (v)

q = make queue ()

Enqueue (q v)

Mark v as visited

While q is not empty

v = dequeue (q)

Process v

for all unvisited vertices v adjacent to v

Mark v as visited

enqueue(qv)

Order in which the nodes are expanded

Complexity

Θ (V2) if represented by an adjacency table

Θ (|V| + |E|) if represented by adjacency lists

Greedy Approach

Greedy algorithms are simple and straightforward

They are shortsighted in their approach in the sense that they take decisions on the basis of information at hand without worrying about the effect these decisions may have in the future

They are easy to invent easy to implement and most of the time quite efficient

Many problems cannot be solved correctly by greedy approach

Greedy algorithms are used to solve optimization problems

Uses of Greedy Approach

Greedy algorithms are often used in ad hoc mobile networking to efficiently route packets with the fewest number of hops and the shortest delay possible

They are also used in machine learning

business intelligence (BI)

artificial intelligence (AI) and

programming

Making Change Algorithm

Huffman Encoding Algorithm

Greedy Approach

Advantages

Very Large number of Feasible Solution

Easy to Implement

Disadvantages

It is much Slower

Does not give optimum result for all problems

Huffman Encoding

Huffman code is a particular type of optimal prefix code that is commonly used for lossless data compression

The process of finding andor using such a code proceeds by means of Huffman codding

The output from Huffmans algorithm can be viewed as a variable-length code table for encoding a source symbol (such as a character in a file)

httpenwikipediaorgwikiHuffman_coding

Huffman Encoding Algorithm

procedure Huffman(f)

Input An array f[1 _ _ _ n] of frequencies

Output An encoding tree with n leaves

let H be a priority queue of integers ordered by f

for i = 1 to n insert(H i)

for k = n + 1 to 2n 1048576 1

i = deletemin(H) j = deletemin(H)

create a node numbered k with children i j

f[k] = f[i] + f[j]

insert(H k)

httpenwikipediaorgwikiHuffman_coding

Working of the Huffman Encoding Algorithm

httpenwikipediaorgwikiHuffman_coding

Complexity

we use a greedy O(nlog(n))

httpwwwsiggraphorgeducationmaterialsHyperGraphvideompegmpegfaqhuffman_tutorialhtml

Knapsack Problem

You have a knapsack that has capacity (weight) W You have several items 1 to n Each item Ij has a weight wj and a benefit bj You want to place a certain number of copies of

each item Ij in the knapsack so that The knapsack weight capacity in not exceeded the total benefits is maximal

httpenwikipediaorgwikiKnapsack_problemApplications

Knapsack Problem

httpenwikipediaorgwikiKnapsack_problemmediaFileKnapsacksvg

Knapsack Problem Variants

01 knapsack Problem The item cannot be divided

Fractional knapsack problem For instance items are liquid or powders

Solvable with a greedy algorithm

httpenwikipediaorgwikiKnapsack_problemApplications

Greedy Knapsack - Fractional

Much Easier For item Ij let ratioj=bjwj This gives you

the benefit per measure of weight Sort the items in descending order of rj If the next item cannot fit into the

knapsack break it and pick it partially just to fill the knapsack

httpenwikipediaorgwikiKnapsack_problemApplications

Greedy Knapsack Algorithm

P and W are arrays contain the profit and weight n objects ordered such that p[i]w[i]=gtp[i+1]w[1+i] that is in decreasing order m is the knapsack size and x is the solution vector

GreedyKnapsack(mn)

bull For(i=0iltni++)

bull X[i]=0

bull Int total=m

bull For (i=0 iltn i++)

bull If(w[i] lt = total)

bull X[i]= 1

bull Total= total + w[i]

bull Else

bull X[i] = total w[i]

bull end Greedyknapsack

httpjava90blogspotcom201202knapsack-problem-in-javahtml

Example - Greedy Knapsack

Item A B C D

Benefit 50 140 60 60

weight 5 20 10 12

Ratio = BW 10 7 6 5

bullExample Knapsack Capacity W = 30 and

httpwwwradfordedu~nokieclasses360greedyhtml

Solution

All of A all of B and ((30-25)10) of C (and none of D) Weight 5 + 20 + 10(510) = 30

Value 50 + 140 + 60(510) = 190 + 30 = 220

httpwwwradfordedu~nokieclasses360greedyhtml

Analysis

If the items are already sorted into descending order of piwi

Then the for-loop takes a time in O(n) Therefore the Total time including the sort is in O(n log

n)

httpjava90blogspotcom201202knapsack-problem-in-javahtml

Backtracking

Backtracking is a technique for systematically trying all paths through a state space

Backtracking search begins at the start state and pursues a path until it reaches either a goal or a ldquodead endrdquo

If it finds a goal it quits and returns the solution path

If it reaches a dead end it ldquobacktracksrdquo to the most recent node on the path

having unexamined siblings and continues down one of these branches

enwikipediaorgwikiBacktracking

Backtracking

Backtracking is an important tool for solving

constraint satisfaction problems such as

crosswords

verbal arithmetic

Sudoku

and many other puzzles

8 Queen Puzzle Algorithm is an example of Backtracking

enwikipediaorgwikiBacktracking

Backtracking

Advantages

Quick test

Pair Matching

Following real life Concept

Disadvantages

Not widely Implemented

Cannot Express Left Recursion

More Time and Complexity

httpwwwclcamacuk~mr10backtrkpdf

Eight queens puzzle

The eight queens puzzle is the problem of placing eight chess queens on an 8times8 chessboard so that no two queens threaten each other

Thus a solution requires that no two queens share the same row column or diagonal

httpenwikipediaorgwikiEight_queens_puzzle

Eight queens puzzle Algorithm

Function backtrack

Begin

SL = [Start] NSL = [Start] DE = [] CS = Start initialize

While NSL ne [ ] do while there are states to be tried

Begin

If CS = goal (or meets goal description) then return SL on success return list of states in path

If CS has no children (excluding nodes already on DE SL and NSL) then

Begin

While SL is not empty and CS = the first element of SL do

Eight queens puzzle Algorithm

Begin

Add CS to DE

Remove first element from SL

Remove first element from NSL

CS ~ first element of NSL

End

Add CS to SL

End

Eight queens puzzle Algorithm

Else begin

Place children of CS (except nodes already on DE SL or NSL) on NSL

CS = first element of NSL

Add CS to SL

End

End

Eight queens puzzle Algorithm

httpwwwcodeprojectcomArticles806248Eight-Queens-Puzzle-Tree-Algorithm

Complexity

The running time of your algorithm is at most N(Nminus1)(Nminus2)⋯(NminusK+1) ie N(NminusK) This is O(NK) ie exponential in K

httpwwwcheggcomhomework-helpquestions-and-answerspoor-man-s-n-queens-problemn-queens-arranged-n-x-n-chessboard-way-queen-checks-queen-queen-q1009394

  • Algorithm Strategies
  • Algorithms Strategy in Assignment
  • Roadmap of Every Strategy
  • Decrease and Conquer
  • Decrease and Conquer
  • Decrease and Conquer
  • Breadth-first search Algorithm
  • Algorithm of BFS
  • Order in which the nodes are expanded
  • Complexity
  • Greedy Approach
  • Uses of Greedy Approach
  • Greedy Approach (2)
  • Huffman Encoding
  • Huffman Encoding Algorithm
  • Working of the Huffman Encoding Algorithm
  • Complexity (2)
  • Knapsack Problem
  • Knapsack Problem
  • Knapsack Problem Variants
  • Greedy Knapsack - Fractional
  • Greedy Knapsack Algorithm
  • Example - Greedy Knapsack
  • Solution
  • Analysis
  • Backtracking
  • Backtracking (2)
  • Backtracking (3)
  • Eight queens puzzle
  • Eight queens puzzle Algorithm
  • Eight queens puzzle Algorithm (2)
  • Eight queens puzzle Algorithm (3)
  • Eight queens puzzle Algorithm
  • Complexity (3)
Page 9: Comparitive Analysis of Algorithm strategies

Order in which the nodes are expanded

Complexity

Θ (V2) if represented by an adjacency table

Θ (|V| + |E|) if represented by adjacency lists

Greedy Approach

Greedy algorithms are simple and straightforward

They are shortsighted in their approach in the sense that they take decisions on the basis of information at hand without worrying about the effect these decisions may have in the future

They are easy to invent easy to implement and most of the time quite efficient

Many problems cannot be solved correctly by greedy approach

Greedy algorithms are used to solve optimization problems

Uses of Greedy Approach

Greedy algorithms are often used in ad hoc mobile networking to efficiently route packets with the fewest number of hops and the shortest delay possible

They are also used in machine learning

business intelligence (BI)

artificial intelligence (AI) and

programming

Making Change Algorithm

Huffman Encoding Algorithm

Greedy Approach

Advantages

Very Large number of Feasible Solution

Easy to Implement

Disadvantages

It is much Slower

Does not give optimum result for all problems

Huffman Encoding

Huffman code is a particular type of optimal prefix code that is commonly used for lossless data compression

The process of finding andor using such a code proceeds by means of Huffman codding

The output from Huffmans algorithm can be viewed as a variable-length code table for encoding a source symbol (such as a character in a file)

httpenwikipediaorgwikiHuffman_coding

Huffman Encoding Algorithm

procedure Huffman(f)

Input An array f[1 _ _ _ n] of frequencies

Output An encoding tree with n leaves

let H be a priority queue of integers ordered by f

for i = 1 to n insert(H i)

for k = n + 1 to 2n 1048576 1

i = deletemin(H) j = deletemin(H)

create a node numbered k with children i j

f[k] = f[i] + f[j]

insert(H k)

httpenwikipediaorgwikiHuffman_coding

Working of the Huffman Encoding Algorithm

httpenwikipediaorgwikiHuffman_coding

Complexity

we use a greedy O(nlog(n))

httpwwwsiggraphorgeducationmaterialsHyperGraphvideompegmpegfaqhuffman_tutorialhtml

Knapsack Problem

You have a knapsack that has capacity (weight) W You have several items 1 to n Each item Ij has a weight wj and a benefit bj You want to place a certain number of copies of

each item Ij in the knapsack so that The knapsack weight capacity in not exceeded the total benefits is maximal

httpenwikipediaorgwikiKnapsack_problemApplications

Knapsack Problem

httpenwikipediaorgwikiKnapsack_problemmediaFileKnapsacksvg

Knapsack Problem Variants

01 knapsack Problem The item cannot be divided

Fractional knapsack problem For instance items are liquid or powders

Solvable with a greedy algorithm

httpenwikipediaorgwikiKnapsack_problemApplications

Greedy Knapsack - Fractional

Much Easier For item Ij let ratioj=bjwj This gives you

the benefit per measure of weight Sort the items in descending order of rj If the next item cannot fit into the

knapsack break it and pick it partially just to fill the knapsack

httpenwikipediaorgwikiKnapsack_problemApplications

Greedy Knapsack Algorithm

P and W are arrays contain the profit and weight n objects ordered such that p[i]w[i]=gtp[i+1]w[1+i] that is in decreasing order m is the knapsack size and x is the solution vector

GreedyKnapsack(mn)

bull For(i=0iltni++)

bull X[i]=0

bull Int total=m

bull For (i=0 iltn i++)

bull If(w[i] lt = total)

bull X[i]= 1

bull Total= total + w[i]

bull Else

bull X[i] = total w[i]

bull end Greedyknapsack

httpjava90blogspotcom201202knapsack-problem-in-javahtml

Example - Greedy Knapsack

Item A B C D

Benefit 50 140 60 60

weight 5 20 10 12

Ratio = BW 10 7 6 5

bullExample Knapsack Capacity W = 30 and

httpwwwradfordedu~nokieclasses360greedyhtml

Solution

All of A all of B and ((30-25)10) of C (and none of D) Weight 5 + 20 + 10(510) = 30

Value 50 + 140 + 60(510) = 190 + 30 = 220

httpwwwradfordedu~nokieclasses360greedyhtml

Analysis

If the items are already sorted into descending order of piwi

Then the for-loop takes a time in O(n) Therefore the Total time including the sort is in O(n log

n)

httpjava90blogspotcom201202knapsack-problem-in-javahtml

Backtracking

Backtracking is a technique for systematically trying all paths through a state space

Backtracking search begins at the start state and pursues a path until it reaches either a goal or a ldquodead endrdquo

If it finds a goal it quits and returns the solution path

If it reaches a dead end it ldquobacktracksrdquo to the most recent node on the path

having unexamined siblings and continues down one of these branches

enwikipediaorgwikiBacktracking

Backtracking

Backtracking is an important tool for solving

constraint satisfaction problems such as

crosswords

verbal arithmetic

Sudoku

and many other puzzles

8 Queen Puzzle Algorithm is an example of Backtracking

enwikipediaorgwikiBacktracking

Backtracking

Advantages

Quick test

Pair Matching

Following real life Concept

Disadvantages

Not widely Implemented

Cannot Express Left Recursion

More Time and Complexity

httpwwwclcamacuk~mr10backtrkpdf

Eight queens puzzle

The eight queens puzzle is the problem of placing eight chess queens on an 8times8 chessboard so that no two queens threaten each other

Thus a solution requires that no two queens share the same row column or diagonal

httpenwikipediaorgwikiEight_queens_puzzle

Eight queens puzzle Algorithm

Function backtrack

Begin

SL = [Start] NSL = [Start] DE = [] CS = Start initialize

While NSL ne [ ] do while there are states to be tried

Begin

If CS = goal (or meets goal description) then return SL on success return list of states in path

If CS has no children (excluding nodes already on DE SL and NSL) then

Begin

While SL is not empty and CS = the first element of SL do

Eight queens puzzle Algorithm

Begin

Add CS to DE

Remove first element from SL

Remove first element from NSL

CS ~ first element of NSL

End

Add CS to SL

End

Eight queens puzzle Algorithm

Else begin

Place children of CS (except nodes already on DE SL or NSL) on NSL

CS = first element of NSL

Add CS to SL

End

End

Eight queens puzzle Algorithm

httpwwwcodeprojectcomArticles806248Eight-Queens-Puzzle-Tree-Algorithm

Complexity

The running time of your algorithm is at most N(Nminus1)(Nminus2)⋯(NminusK+1) ie N(NminusK) This is O(NK) ie exponential in K

httpwwwcheggcomhomework-helpquestions-and-answerspoor-man-s-n-queens-problemn-queens-arranged-n-x-n-chessboard-way-queen-checks-queen-queen-q1009394

  • Algorithm Strategies
  • Algorithms Strategy in Assignment
  • Roadmap of Every Strategy
  • Decrease and Conquer
  • Decrease and Conquer
  • Decrease and Conquer
  • Breadth-first search Algorithm
  • Algorithm of BFS
  • Order in which the nodes are expanded
  • Complexity
  • Greedy Approach
  • Uses of Greedy Approach
  • Greedy Approach (2)
  • Huffman Encoding
  • Huffman Encoding Algorithm
  • Working of the Huffman Encoding Algorithm
  • Complexity (2)
  • Knapsack Problem
  • Knapsack Problem
  • Knapsack Problem Variants
  • Greedy Knapsack - Fractional
  • Greedy Knapsack Algorithm
  • Example - Greedy Knapsack
  • Solution
  • Analysis
  • Backtracking
  • Backtracking (2)
  • Backtracking (3)
  • Eight queens puzzle
  • Eight queens puzzle Algorithm
  • Eight queens puzzle Algorithm (2)
  • Eight queens puzzle Algorithm (3)
  • Eight queens puzzle Algorithm
  • Complexity (3)
Page 10: Comparitive Analysis of Algorithm strategies

Complexity

Θ (V2) if represented by an adjacency table

Θ (|V| + |E|) if represented by adjacency lists

Greedy Approach

Greedy algorithms are simple and straightforward

They are shortsighted in their approach in the sense that they take decisions on the basis of information at hand without worrying about the effect these decisions may have in the future

They are easy to invent easy to implement and most of the time quite efficient

Many problems cannot be solved correctly by greedy approach

Greedy algorithms are used to solve optimization problems

Uses of Greedy Approach

Greedy algorithms are often used in ad hoc mobile networking to efficiently route packets with the fewest number of hops and the shortest delay possible

They are also used in machine learning

business intelligence (BI)

artificial intelligence (AI) and

programming

Making Change Algorithm

Huffman Encoding Algorithm

Greedy Approach

Advantages

Very Large number of Feasible Solution

Easy to Implement

Disadvantages

It is much Slower

Does not give optimum result for all problems

Huffman Encoding

Huffman code is a particular type of optimal prefix code that is commonly used for lossless data compression

The process of finding andor using such a code proceeds by means of Huffman codding

The output from Huffmans algorithm can be viewed as a variable-length code table for encoding a source symbol (such as a character in a file)

httpenwikipediaorgwikiHuffman_coding

Huffman Encoding Algorithm

procedure Huffman(f)

Input An array f[1 _ _ _ n] of frequencies

Output An encoding tree with n leaves

let H be a priority queue of integers ordered by f

for i = 1 to n insert(H i)

for k = n + 1 to 2n 1048576 1

i = deletemin(H) j = deletemin(H)

create a node numbered k with children i j

f[k] = f[i] + f[j]

insert(H k)

httpenwikipediaorgwikiHuffman_coding

Working of the Huffman Encoding Algorithm

httpenwikipediaorgwikiHuffman_coding

Complexity

we use a greedy O(nlog(n))

httpwwwsiggraphorgeducationmaterialsHyperGraphvideompegmpegfaqhuffman_tutorialhtml

Knapsack Problem

You have a knapsack that has capacity (weight) W You have several items 1 to n Each item Ij has a weight wj and a benefit bj You want to place a certain number of copies of

each item Ij in the knapsack so that The knapsack weight capacity in not exceeded the total benefits is maximal

httpenwikipediaorgwikiKnapsack_problemApplications

Knapsack Problem

httpenwikipediaorgwikiKnapsack_problemmediaFileKnapsacksvg

Knapsack Problem Variants

01 knapsack Problem The item cannot be divided

Fractional knapsack problem For instance items are liquid or powders

Solvable with a greedy algorithm

httpenwikipediaorgwikiKnapsack_problemApplications

Greedy Knapsack - Fractional

Much Easier For item Ij let ratioj=bjwj This gives you

the benefit per measure of weight Sort the items in descending order of rj If the next item cannot fit into the

knapsack break it and pick it partially just to fill the knapsack

httpenwikipediaorgwikiKnapsack_problemApplications

Greedy Knapsack Algorithm

P and W are arrays contain the profit and weight n objects ordered such that p[i]w[i]=gtp[i+1]w[1+i] that is in decreasing order m is the knapsack size and x is the solution vector

GreedyKnapsack(mn)

bull For(i=0iltni++)

bull X[i]=0

bull Int total=m

bull For (i=0 iltn i++)

bull If(w[i] lt = total)

bull X[i]= 1

bull Total= total + w[i]

bull Else

bull X[i] = total w[i]

bull end Greedyknapsack

httpjava90blogspotcom201202knapsack-problem-in-javahtml

Example - Greedy Knapsack

Item A B C D

Benefit 50 140 60 60

weight 5 20 10 12

Ratio = BW 10 7 6 5

bullExample Knapsack Capacity W = 30 and

httpwwwradfordedu~nokieclasses360greedyhtml

Solution

All of A all of B and ((30-25)10) of C (and none of D) Weight 5 + 20 + 10(510) = 30

Value 50 + 140 + 60(510) = 190 + 30 = 220

httpwwwradfordedu~nokieclasses360greedyhtml

Analysis

If the items are already sorted into descending order of piwi

Then the for-loop takes a time in O(n) Therefore the Total time including the sort is in O(n log

n)

httpjava90blogspotcom201202knapsack-problem-in-javahtml

Backtracking

Backtracking is a technique for systematically trying all paths through a state space

Backtracking search begins at the start state and pursues a path until it reaches either a goal or a ldquodead endrdquo

If it finds a goal it quits and returns the solution path

If it reaches a dead end it ldquobacktracksrdquo to the most recent node on the path

having unexamined siblings and continues down one of these branches

enwikipediaorgwikiBacktracking

Backtracking

Backtracking is an important tool for solving

constraint satisfaction problems such as

crosswords

verbal arithmetic

Sudoku

and many other puzzles

8 Queen Puzzle Algorithm is an example of Backtracking

enwikipediaorgwikiBacktracking

Backtracking

Advantages

Quick test

Pair Matching

Following real life Concept

Disadvantages

Not widely Implemented

Cannot Express Left Recursion

More Time and Complexity

httpwwwclcamacuk~mr10backtrkpdf

Eight queens puzzle

The eight queens puzzle is the problem of placing eight chess queens on an 8times8 chessboard so that no two queens threaten each other

Thus a solution requires that no two queens share the same row column or diagonal

httpenwikipediaorgwikiEight_queens_puzzle

Eight queens puzzle Algorithm

Function backtrack

Begin

SL = [Start] NSL = [Start] DE = [] CS = Start initialize

While NSL ne [ ] do while there are states to be tried

Begin

If CS = goal (or meets goal description) then return SL on success return list of states in path

If CS has no children (excluding nodes already on DE SL and NSL) then

Begin

While SL is not empty and CS = the first element of SL do

Eight queens puzzle Algorithm

Begin

Add CS to DE

Remove first element from SL

Remove first element from NSL

CS ~ first element of NSL

End

Add CS to SL

End

Eight queens puzzle Algorithm

Else begin

Place children of CS (except nodes already on DE SL or NSL) on NSL

CS = first element of NSL

Add CS to SL

End

End

Eight queens puzzle Algorithm

httpwwwcodeprojectcomArticles806248Eight-Queens-Puzzle-Tree-Algorithm

Complexity

The running time of your algorithm is at most N(Nminus1)(Nminus2)⋯(NminusK+1) ie N(NminusK) This is O(NK) ie exponential in K

httpwwwcheggcomhomework-helpquestions-and-answerspoor-man-s-n-queens-problemn-queens-arranged-n-x-n-chessboard-way-queen-checks-queen-queen-q1009394

  • Algorithm Strategies
  • Algorithms Strategy in Assignment
  • Roadmap of Every Strategy
  • Decrease and Conquer
  • Decrease and Conquer
  • Decrease and Conquer
  • Breadth-first search Algorithm
  • Algorithm of BFS
  • Order in which the nodes are expanded
  • Complexity
  • Greedy Approach
  • Uses of Greedy Approach
  • Greedy Approach (2)
  • Huffman Encoding
  • Huffman Encoding Algorithm
  • Working of the Huffman Encoding Algorithm
  • Complexity (2)
  • Knapsack Problem
  • Knapsack Problem
  • Knapsack Problem Variants
  • Greedy Knapsack - Fractional
  • Greedy Knapsack Algorithm
  • Example - Greedy Knapsack
  • Solution
  • Analysis
  • Backtracking
  • Backtracking (2)
  • Backtracking (3)
  • Eight queens puzzle
  • Eight queens puzzle Algorithm
  • Eight queens puzzle Algorithm (2)
  • Eight queens puzzle Algorithm (3)
  • Eight queens puzzle Algorithm
  • Complexity (3)
Page 11: Comparitive Analysis of Algorithm strategies

Greedy Approach

Greedy algorithms are simple and straightforward

They are shortsighted in their approach in the sense that they take decisions on the basis of information at hand without worrying about the effect these decisions may have in the future

They are easy to invent easy to implement and most of the time quite efficient

Many problems cannot be solved correctly by greedy approach

Greedy algorithms are used to solve optimization problems

Uses of Greedy Approach

Greedy algorithms are often used in ad hoc mobile networking to efficiently route packets with the fewest number of hops and the shortest delay possible

They are also used in machine learning

business intelligence (BI)

artificial intelligence (AI) and

programming

Making Change Algorithm

Huffman Encoding Algorithm

Greedy Approach

Advantages

Very Large number of Feasible Solution

Easy to Implement

Disadvantages

It is much Slower

Does not give optimum result for all problems

Huffman Encoding

Huffman code is a particular type of optimal prefix code that is commonly used for lossless data compression

The process of finding andor using such a code proceeds by means of Huffman codding

The output from Huffmans algorithm can be viewed as a variable-length code table for encoding a source symbol (such as a character in a file)

httpenwikipediaorgwikiHuffman_coding

Huffman Encoding Algorithm

procedure Huffman(f)

Input An array f[1 _ _ _ n] of frequencies

Output An encoding tree with n leaves

let H be a priority queue of integers ordered by f

for i = 1 to n insert(H i)

for k = n + 1 to 2n 1048576 1

i = deletemin(H) j = deletemin(H)

create a node numbered k with children i j

f[k] = f[i] + f[j]

insert(H k)

httpenwikipediaorgwikiHuffman_coding

Working of the Huffman Encoding Algorithm

httpenwikipediaorgwikiHuffman_coding

Complexity

we use a greedy O(nlog(n))

httpwwwsiggraphorgeducationmaterialsHyperGraphvideompegmpegfaqhuffman_tutorialhtml

Knapsack Problem

You have a knapsack that has capacity (weight) W You have several items 1 to n Each item Ij has a weight wj and a benefit bj You want to place a certain number of copies of

each item Ij in the knapsack so that The knapsack weight capacity in not exceeded the total benefits is maximal

httpenwikipediaorgwikiKnapsack_problemApplications

Knapsack Problem

httpenwikipediaorgwikiKnapsack_problemmediaFileKnapsacksvg

Knapsack Problem Variants

01 knapsack Problem The item cannot be divided

Fractional knapsack problem For instance items are liquid or powders

Solvable with a greedy algorithm

httpenwikipediaorgwikiKnapsack_problemApplications

Greedy Knapsack - Fractional

Much Easier For item Ij let ratioj=bjwj This gives you

the benefit per measure of weight Sort the items in descending order of rj If the next item cannot fit into the

knapsack break it and pick it partially just to fill the knapsack

httpenwikipediaorgwikiKnapsack_problemApplications

Greedy Knapsack Algorithm

P and W are arrays contain the profit and weight n objects ordered such that p[i]w[i]=gtp[i+1]w[1+i] that is in decreasing order m is the knapsack size and x is the solution vector

GreedyKnapsack(mn)

bull For(i=0iltni++)

bull X[i]=0

bull Int total=m

bull For (i=0 iltn i++)

bull If(w[i] lt = total)

bull X[i]= 1

bull Total= total + w[i]

bull Else

bull X[i] = total w[i]

bull end Greedyknapsack

httpjava90blogspotcom201202knapsack-problem-in-javahtml

Example - Greedy Knapsack

Item A B C D

Benefit 50 140 60 60

weight 5 20 10 12

Ratio = BW 10 7 6 5

bullExample Knapsack Capacity W = 30 and

httpwwwradfordedu~nokieclasses360greedyhtml

Solution

All of A all of B and ((30-25)10) of C (and none of D) Weight 5 + 20 + 10(510) = 30

Value 50 + 140 + 60(510) = 190 + 30 = 220

httpwwwradfordedu~nokieclasses360greedyhtml

Analysis

If the items are already sorted into descending order of piwi

Then the for-loop takes a time in O(n) Therefore the Total time including the sort is in O(n log

n)

httpjava90blogspotcom201202knapsack-problem-in-javahtml

Backtracking

Backtracking is a technique for systematically trying all paths through a state space

Backtracking search begins at the start state and pursues a path until it reaches either a goal or a ldquodead endrdquo

If it finds a goal it quits and returns the solution path

If it reaches a dead end it ldquobacktracksrdquo to the most recent node on the path

having unexamined siblings and continues down one of these branches

enwikipediaorgwikiBacktracking

Backtracking

Backtracking is an important tool for solving

constraint satisfaction problems such as

crosswords

verbal arithmetic

Sudoku

and many other puzzles

8 Queen Puzzle Algorithm is an example of Backtracking

enwikipediaorgwikiBacktracking

Backtracking

Advantages

Quick test

Pair Matching

Following real life Concept

Disadvantages

Not widely Implemented

Cannot Express Left Recursion

More Time and Complexity

httpwwwclcamacuk~mr10backtrkpdf

Eight queens puzzle

The eight queens puzzle is the problem of placing eight chess queens on an 8times8 chessboard so that no two queens threaten each other

Thus a solution requires that no two queens share the same row column or diagonal

httpenwikipediaorgwikiEight_queens_puzzle

Eight queens puzzle Algorithm

Function backtrack

Begin

SL = [Start] NSL = [Start] DE = [] CS = Start initialize

While NSL ne [ ] do while there are states to be tried

Begin

If CS = goal (or meets goal description) then return SL on success return list of states in path

If CS has no children (excluding nodes already on DE SL and NSL) then

Begin

While SL is not empty and CS = the first element of SL do

Eight queens puzzle Algorithm

Begin

Add CS to DE

Remove first element from SL

Remove first element from NSL

CS ~ first element of NSL

End

Add CS to SL

End

Eight queens puzzle Algorithm

Else begin

Place children of CS (except nodes already on DE SL or NSL) on NSL

CS = first element of NSL

Add CS to SL

End

End

Eight queens puzzle Algorithm

httpwwwcodeprojectcomArticles806248Eight-Queens-Puzzle-Tree-Algorithm

Complexity

The running time of your algorithm is at most N(Nminus1)(Nminus2)⋯(NminusK+1) ie N(NminusK) This is O(NK) ie exponential in K

httpwwwcheggcomhomework-helpquestions-and-answerspoor-man-s-n-queens-problemn-queens-arranged-n-x-n-chessboard-way-queen-checks-queen-queen-q1009394

  • Algorithm Strategies
  • Algorithms Strategy in Assignment
  • Roadmap of Every Strategy
  • Decrease and Conquer
  • Decrease and Conquer
  • Decrease and Conquer
  • Breadth-first search Algorithm
  • Algorithm of BFS
  • Order in which the nodes are expanded
  • Complexity
  • Greedy Approach
  • Uses of Greedy Approach
  • Greedy Approach (2)
  • Huffman Encoding
  • Huffman Encoding Algorithm
  • Working of the Huffman Encoding Algorithm
  • Complexity (2)
  • Knapsack Problem
  • Knapsack Problem
  • Knapsack Problem Variants
  • Greedy Knapsack - Fractional
  • Greedy Knapsack Algorithm
  • Example - Greedy Knapsack
  • Solution
  • Analysis
  • Backtracking
  • Backtracking (2)
  • Backtracking (3)
  • Eight queens puzzle
  • Eight queens puzzle Algorithm
  • Eight queens puzzle Algorithm (2)
  • Eight queens puzzle Algorithm (3)
  • Eight queens puzzle Algorithm
  • Complexity (3)
Page 12: Comparitive Analysis of Algorithm strategies

Uses of Greedy Approach

Greedy algorithms are often used in ad hoc mobile networking to efficiently route packets with the fewest number of hops and the shortest delay possible

They are also used in machine learning

business intelligence (BI)

artificial intelligence (AI) and

programming

Making Change Algorithm

Huffman Encoding Algorithm

Greedy Approach

Advantages

Very Large number of Feasible Solution

Easy to Implement

Disadvantages

It is much Slower

Does not give optimum result for all problems

Huffman Encoding

Huffman code is a particular type of optimal prefix code that is commonly used for lossless data compression

The process of finding andor using such a code proceeds by means of Huffman codding

The output from Huffmans algorithm can be viewed as a variable-length code table for encoding a source symbol (such as a character in a file)

httpenwikipediaorgwikiHuffman_coding

Huffman Encoding Algorithm

procedure Huffman(f)

Input An array f[1 _ _ _ n] of frequencies

Output An encoding tree with n leaves

let H be a priority queue of integers ordered by f

for i = 1 to n insert(H i)

for k = n + 1 to 2n 1048576 1

i = deletemin(H) j = deletemin(H)

create a node numbered k with children i j

f[k] = f[i] + f[j]

insert(H k)

httpenwikipediaorgwikiHuffman_coding

Working of the Huffman Encoding Algorithm

httpenwikipediaorgwikiHuffman_coding

Complexity

we use a greedy O(nlog(n))

httpwwwsiggraphorgeducationmaterialsHyperGraphvideompegmpegfaqhuffman_tutorialhtml

Knapsack Problem

You have a knapsack that has capacity (weight) W You have several items 1 to n Each item Ij has a weight wj and a benefit bj You want to place a certain number of copies of

each item Ij in the knapsack so that The knapsack weight capacity in not exceeded the total benefits is maximal

httpenwikipediaorgwikiKnapsack_problemApplications

Knapsack Problem

httpenwikipediaorgwikiKnapsack_problemmediaFileKnapsacksvg

Knapsack Problem Variants

01 knapsack Problem The item cannot be divided

Fractional knapsack problem For instance items are liquid or powders

Solvable with a greedy algorithm

httpenwikipediaorgwikiKnapsack_problemApplications

Greedy Knapsack - Fractional

Much Easier For item Ij let ratioj=bjwj This gives you

the benefit per measure of weight Sort the items in descending order of rj If the next item cannot fit into the

knapsack break it and pick it partially just to fill the knapsack

httpenwikipediaorgwikiKnapsack_problemApplications

Greedy Knapsack Algorithm

P and W are arrays contain the profit and weight n objects ordered such that p[i]w[i]=gtp[i+1]w[1+i] that is in decreasing order m is the knapsack size and x is the solution vector

GreedyKnapsack(mn)

bull For(i=0iltni++)

bull X[i]=0

bull Int total=m

bull For (i=0 iltn i++)

bull If(w[i] lt = total)

bull X[i]= 1

bull Total= total + w[i]

bull Else

bull X[i] = total w[i]

bull end Greedyknapsack

httpjava90blogspotcom201202knapsack-problem-in-javahtml

Example - Greedy Knapsack

Item A B C D

Benefit 50 140 60 60

weight 5 20 10 12

Ratio = BW 10 7 6 5

bullExample Knapsack Capacity W = 30 and

httpwwwradfordedu~nokieclasses360greedyhtml

Solution

All of A all of B and ((30-25)10) of C (and none of D) Weight 5 + 20 + 10(510) = 30

Value 50 + 140 + 60(510) = 190 + 30 = 220

httpwwwradfordedu~nokieclasses360greedyhtml

Analysis

If the items are already sorted into descending order of piwi

Then the for-loop takes a time in O(n) Therefore the Total time including the sort is in O(n log

n)

httpjava90blogspotcom201202knapsack-problem-in-javahtml

Backtracking

Backtracking is a technique for systematically trying all paths through a state space

Backtracking search begins at the start state and pursues a path until it reaches either a goal or a ldquodead endrdquo

If it finds a goal it quits and returns the solution path

If it reaches a dead end it ldquobacktracksrdquo to the most recent node on the path

having unexamined siblings and continues down one of these branches

enwikipediaorgwikiBacktracking

Backtracking

Backtracking is an important tool for solving

constraint satisfaction problems such as

crosswords

verbal arithmetic

Sudoku

and many other puzzles

8 Queen Puzzle Algorithm is an example of Backtracking

enwikipediaorgwikiBacktracking

Backtracking

Advantages

Quick test

Pair Matching

Following real life Concept

Disadvantages

Not widely Implemented

Cannot Express Left Recursion

More Time and Complexity

httpwwwclcamacuk~mr10backtrkpdf

Eight queens puzzle

The eight queens puzzle is the problem of placing eight chess queens on an 8times8 chessboard so that no two queens threaten each other

Thus a solution requires that no two queens share the same row column or diagonal

httpenwikipediaorgwikiEight_queens_puzzle

Eight queens puzzle Algorithm

Function backtrack

Begin

SL = [Start] NSL = [Start] DE = [] CS = Start initialize

While NSL ne [ ] do while there are states to be tried

Begin

If CS = goal (or meets goal description) then return SL on success return list of states in path

If CS has no children (excluding nodes already on DE SL and NSL) then

Begin

While SL is not empty and CS = the first element of SL do

Eight queens puzzle Algorithm

Begin

Add CS to DE

Remove first element from SL

Remove first element from NSL

CS ~ first element of NSL

End

Add CS to SL

End

Eight queens puzzle Algorithm

Else begin

Place children of CS (except nodes already on DE SL or NSL) on NSL

CS = first element of NSL

Add CS to SL

End

End

Eight queens puzzle Algorithm

httpwwwcodeprojectcomArticles806248Eight-Queens-Puzzle-Tree-Algorithm

Complexity

The running time of your algorithm is at most N(Nminus1)(Nminus2)⋯(NminusK+1) ie N(NminusK) This is O(NK) ie exponential in K

httpwwwcheggcomhomework-helpquestions-and-answerspoor-man-s-n-queens-problemn-queens-arranged-n-x-n-chessboard-way-queen-checks-queen-queen-q1009394

  • Algorithm Strategies
  • Algorithms Strategy in Assignment
  • Roadmap of Every Strategy
  • Decrease and Conquer
  • Decrease and Conquer
  • Decrease and Conquer
  • Breadth-first search Algorithm
  • Algorithm of BFS
  • Order in which the nodes are expanded
  • Complexity
  • Greedy Approach
  • Uses of Greedy Approach
  • Greedy Approach (2)
  • Huffman Encoding
  • Huffman Encoding Algorithm
  • Working of the Huffman Encoding Algorithm
  • Complexity (2)
  • Knapsack Problem
  • Knapsack Problem
  • Knapsack Problem Variants
  • Greedy Knapsack - Fractional
  • Greedy Knapsack Algorithm
  • Example - Greedy Knapsack
  • Solution
  • Analysis
  • Backtracking
  • Backtracking (2)
  • Backtracking (3)
  • Eight queens puzzle
  • Eight queens puzzle Algorithm
  • Eight queens puzzle Algorithm (2)
  • Eight queens puzzle Algorithm (3)
  • Eight queens puzzle Algorithm
  • Complexity (3)
Page 13: Comparitive Analysis of Algorithm strategies

Greedy Approach

Advantages

Very Large number of Feasible Solution

Easy to Implement

Disadvantages

It is much Slower

Does not give optimum result for all problems

Huffman Encoding

Huffman code is a particular type of optimal prefix code that is commonly used for lossless data compression

The process of finding andor using such a code proceeds by means of Huffman codding

The output from Huffmans algorithm can be viewed as a variable-length code table for encoding a source symbol (such as a character in a file)

httpenwikipediaorgwikiHuffman_coding

Huffman Encoding Algorithm

procedure Huffman(f)

Input An array f[1 _ _ _ n] of frequencies

Output An encoding tree with n leaves

let H be a priority queue of integers ordered by f

for i = 1 to n insert(H i)

for k = n + 1 to 2n 1048576 1

i = deletemin(H) j = deletemin(H)

create a node numbered k with children i j

f[k] = f[i] + f[j]

insert(H k)

httpenwikipediaorgwikiHuffman_coding

Working of the Huffman Encoding Algorithm

httpenwikipediaorgwikiHuffman_coding

Complexity

we use a greedy O(nlog(n))

httpwwwsiggraphorgeducationmaterialsHyperGraphvideompegmpegfaqhuffman_tutorialhtml

Knapsack Problem

You have a knapsack that has capacity (weight) W You have several items 1 to n Each item Ij has a weight wj and a benefit bj You want to place a certain number of copies of

each item Ij in the knapsack so that The knapsack weight capacity in not exceeded the total benefits is maximal

httpenwikipediaorgwikiKnapsack_problemApplications

Knapsack Problem

httpenwikipediaorgwikiKnapsack_problemmediaFileKnapsacksvg

Knapsack Problem Variants

01 knapsack Problem The item cannot be divided

Fractional knapsack problem For instance items are liquid or powders

Solvable with a greedy algorithm

httpenwikipediaorgwikiKnapsack_problemApplications

Greedy Knapsack - Fractional

Much Easier For item Ij let ratioj=bjwj This gives you

the benefit per measure of weight Sort the items in descending order of rj If the next item cannot fit into the

knapsack break it and pick it partially just to fill the knapsack

httpenwikipediaorgwikiKnapsack_problemApplications

Greedy Knapsack Algorithm

P and W are arrays contain the profit and weight n objects ordered such that p[i]w[i]=gtp[i+1]w[1+i] that is in decreasing order m is the knapsack size and x is the solution vector

GreedyKnapsack(mn)

bull For(i=0iltni++)

bull X[i]=0

bull Int total=m

bull For (i=0 iltn i++)

bull If(w[i] lt = total)

bull X[i]= 1

bull Total= total + w[i]

bull Else

bull X[i] = total w[i]

bull end Greedyknapsack

httpjava90blogspotcom201202knapsack-problem-in-javahtml

Example - Greedy Knapsack

Item A B C D

Benefit 50 140 60 60

weight 5 20 10 12

Ratio = BW 10 7 6 5

bullExample Knapsack Capacity W = 30 and

httpwwwradfordedu~nokieclasses360greedyhtml

Solution

All of A all of B and ((30-25)10) of C (and none of D) Weight 5 + 20 + 10(510) = 30

Value 50 + 140 + 60(510) = 190 + 30 = 220

httpwwwradfordedu~nokieclasses360greedyhtml

Analysis

If the items are already sorted into descending order of piwi

Then the for-loop takes a time in O(n) Therefore the Total time including the sort is in O(n log

n)

httpjava90blogspotcom201202knapsack-problem-in-javahtml

Backtracking

Backtracking is a technique for systematically trying all paths through a state space

Backtracking search begins at the start state and pursues a path until it reaches either a goal or a ldquodead endrdquo

If it finds a goal it quits and returns the solution path

If it reaches a dead end it ldquobacktracksrdquo to the most recent node on the path

having unexamined siblings and continues down one of these branches

enwikipediaorgwikiBacktracking

Backtracking

Backtracking is an important tool for solving

constraint satisfaction problems such as

crosswords

verbal arithmetic

Sudoku

and many other puzzles

8 Queen Puzzle Algorithm is an example of Backtracking

enwikipediaorgwikiBacktracking

Backtracking

Advantages

Quick test

Pair Matching

Following real life Concept

Disadvantages

Not widely Implemented

Cannot Express Left Recursion

More Time and Complexity

httpwwwclcamacuk~mr10backtrkpdf

Eight queens puzzle

The eight queens puzzle is the problem of placing eight chess queens on an 8times8 chessboard so that no two queens threaten each other

Thus a solution requires that no two queens share the same row column or diagonal

httpenwikipediaorgwikiEight_queens_puzzle

Eight queens puzzle Algorithm

Function backtrack

Begin

SL = [Start] NSL = [Start] DE = [] CS = Start initialize

While NSL ne [ ] do while there are states to be tried

Begin

If CS = goal (or meets goal description) then return SL on success return list of states in path

If CS has no children (excluding nodes already on DE SL and NSL) then

Begin

While SL is not empty and CS = the first element of SL do

Eight queens puzzle Algorithm

Begin

Add CS to DE

Remove first element from SL

Remove first element from NSL

CS ~ first element of NSL

End

Add CS to SL

End

Eight queens puzzle Algorithm

Else begin

Place children of CS (except nodes already on DE SL or NSL) on NSL

CS = first element of NSL

Add CS to SL

End

End

Eight queens puzzle Algorithm

httpwwwcodeprojectcomArticles806248Eight-Queens-Puzzle-Tree-Algorithm

Complexity

The running time of your algorithm is at most N(Nminus1)(Nminus2)⋯(NminusK+1) ie N(NminusK) This is O(NK) ie exponential in K

httpwwwcheggcomhomework-helpquestions-and-answerspoor-man-s-n-queens-problemn-queens-arranged-n-x-n-chessboard-way-queen-checks-queen-queen-q1009394

  • Algorithm Strategies
  • Algorithms Strategy in Assignment
  • Roadmap of Every Strategy
  • Decrease and Conquer
  • Decrease and Conquer
  • Decrease and Conquer
  • Breadth-first search Algorithm
  • Algorithm of BFS
  • Order in which the nodes are expanded
  • Complexity
  • Greedy Approach
  • Uses of Greedy Approach
  • Greedy Approach (2)
  • Huffman Encoding
  • Huffman Encoding Algorithm
  • Working of the Huffman Encoding Algorithm
  • Complexity (2)
  • Knapsack Problem
  • Knapsack Problem
  • Knapsack Problem Variants
  • Greedy Knapsack - Fractional
  • Greedy Knapsack Algorithm
  • Example - Greedy Knapsack
  • Solution
  • Analysis
  • Backtracking
  • Backtracking (2)
  • Backtracking (3)
  • Eight queens puzzle
  • Eight queens puzzle Algorithm
  • Eight queens puzzle Algorithm (2)
  • Eight queens puzzle Algorithm (3)
  • Eight queens puzzle Algorithm
  • Complexity (3)
Page 14: Comparitive Analysis of Algorithm strategies

Huffman Encoding

Huffman code is a particular type of optimal prefix code that is commonly used for lossless data compression

The process of finding andor using such a code proceeds by means of Huffman codding

The output from Huffmans algorithm can be viewed as a variable-length code table for encoding a source symbol (such as a character in a file)

httpenwikipediaorgwikiHuffman_coding

Huffman Encoding Algorithm

procedure Huffman(f)

Input An array f[1 _ _ _ n] of frequencies

Output An encoding tree with n leaves

let H be a priority queue of integers ordered by f

for i = 1 to n insert(H i)

for k = n + 1 to 2n 1048576 1

i = deletemin(H) j = deletemin(H)

create a node numbered k with children i j

f[k] = f[i] + f[j]

insert(H k)

httpenwikipediaorgwikiHuffman_coding

Working of the Huffman Encoding Algorithm

httpenwikipediaorgwikiHuffman_coding

Complexity

we use a greedy O(nlog(n))

httpwwwsiggraphorgeducationmaterialsHyperGraphvideompegmpegfaqhuffman_tutorialhtml

Knapsack Problem

You have a knapsack that has capacity (weight) W You have several items 1 to n Each item Ij has a weight wj and a benefit bj You want to place a certain number of copies of

each item Ij in the knapsack so that The knapsack weight capacity in not exceeded the total benefits is maximal

httpenwikipediaorgwikiKnapsack_problemApplications

Knapsack Problem

httpenwikipediaorgwikiKnapsack_problemmediaFileKnapsacksvg

Knapsack Problem Variants

01 knapsack Problem The item cannot be divided

Fractional knapsack problem For instance items are liquid or powders

Solvable with a greedy algorithm

httpenwikipediaorgwikiKnapsack_problemApplications

Greedy Knapsack - Fractional

Much Easier For item Ij let ratioj=bjwj This gives you

the benefit per measure of weight Sort the items in descending order of rj If the next item cannot fit into the

knapsack break it and pick it partially just to fill the knapsack

httpenwikipediaorgwikiKnapsack_problemApplications

Greedy Knapsack Algorithm

P and W are arrays contain the profit and weight n objects ordered such that p[i]w[i]=gtp[i+1]w[1+i] that is in decreasing order m is the knapsack size and x is the solution vector

GreedyKnapsack(mn)

bull For(i=0iltni++)

bull X[i]=0

bull Int total=m

bull For (i=0 iltn i++)

bull If(w[i] lt = total)

bull X[i]= 1

bull Total= total + w[i]

bull Else

bull X[i] = total w[i]

bull end Greedyknapsack

httpjava90blogspotcom201202knapsack-problem-in-javahtml

Example - Greedy Knapsack

Item A B C D

Benefit 50 140 60 60

weight 5 20 10 12

Ratio = BW 10 7 6 5

bullExample Knapsack Capacity W = 30 and

httpwwwradfordedu~nokieclasses360greedyhtml

Solution

All of A all of B and ((30-25)10) of C (and none of D) Weight 5 + 20 + 10(510) = 30

Value 50 + 140 + 60(510) = 190 + 30 = 220

httpwwwradfordedu~nokieclasses360greedyhtml

Analysis

If the items are already sorted into descending order of piwi

Then the for-loop takes a time in O(n) Therefore the Total time including the sort is in O(n log

n)

httpjava90blogspotcom201202knapsack-problem-in-javahtml

Backtracking

Backtracking is a technique for systematically trying all paths through a state space

Backtracking search begins at the start state and pursues a path until it reaches either a goal or a ldquodead endrdquo

If it finds a goal it quits and returns the solution path

If it reaches a dead end it ldquobacktracksrdquo to the most recent node on the path

having unexamined siblings and continues down one of these branches

enwikipediaorgwikiBacktracking

Backtracking

Backtracking is an important tool for solving

constraint satisfaction problems such as

crosswords

verbal arithmetic

Sudoku

and many other puzzles

8 Queen Puzzle Algorithm is an example of Backtracking

enwikipediaorgwikiBacktracking

Backtracking

Advantages

Quick test

Pair Matching

Following real life Concept

Disadvantages

Not widely Implemented

Cannot Express Left Recursion

More Time and Complexity

httpwwwclcamacuk~mr10backtrkpdf

Eight queens puzzle

The eight queens puzzle is the problem of placing eight chess queens on an 8times8 chessboard so that no two queens threaten each other

Thus a solution requires that no two queens share the same row column or diagonal

httpenwikipediaorgwikiEight_queens_puzzle

Eight queens puzzle Algorithm

Function backtrack

Begin

SL = [Start] NSL = [Start] DE = [] CS = Start initialize

While NSL ne [ ] do while there are states to be tried

Begin

If CS = goal (or meets goal description) then return SL on success return list of states in path

If CS has no children (excluding nodes already on DE SL and NSL) then

Begin

While SL is not empty and CS = the first element of SL do

Eight queens puzzle Algorithm

Begin

Add CS to DE

Remove first element from SL

Remove first element from NSL

CS ~ first element of NSL

End

Add CS to SL

End

Eight queens puzzle Algorithm

Else begin

Place children of CS (except nodes already on DE SL or NSL) on NSL

CS = first element of NSL

Add CS to SL

End

End

Eight queens puzzle Algorithm

httpwwwcodeprojectcomArticles806248Eight-Queens-Puzzle-Tree-Algorithm

Complexity

The running time of your algorithm is at most N(Nminus1)(Nminus2)⋯(NminusK+1) ie N(NminusK) This is O(NK) ie exponential in K

httpwwwcheggcomhomework-helpquestions-and-answerspoor-man-s-n-queens-problemn-queens-arranged-n-x-n-chessboard-way-queen-checks-queen-queen-q1009394

  • Algorithm Strategies
  • Algorithms Strategy in Assignment
  • Roadmap of Every Strategy
  • Decrease and Conquer
  • Decrease and Conquer
  • Decrease and Conquer
  • Breadth-first search Algorithm
  • Algorithm of BFS
  • Order in which the nodes are expanded
  • Complexity
  • Greedy Approach
  • Uses of Greedy Approach
  • Greedy Approach (2)
  • Huffman Encoding
  • Huffman Encoding Algorithm
  • Working of the Huffman Encoding Algorithm
  • Complexity (2)
  • Knapsack Problem
  • Knapsack Problem
  • Knapsack Problem Variants
  • Greedy Knapsack - Fractional
  • Greedy Knapsack Algorithm
  • Example - Greedy Knapsack
  • Solution
  • Analysis
  • Backtracking
  • Backtracking (2)
  • Backtracking (3)
  • Eight queens puzzle
  • Eight queens puzzle Algorithm
  • Eight queens puzzle Algorithm (2)
  • Eight queens puzzle Algorithm (3)
  • Eight queens puzzle Algorithm
  • Complexity (3)
Page 15: Comparitive Analysis of Algorithm strategies

Huffman Encoding Algorithm

procedure Huffman(f)

Input An array f[1 _ _ _ n] of frequencies

Output An encoding tree with n leaves

let H be a priority queue of integers ordered by f

for i = 1 to n insert(H i)

for k = n + 1 to 2n 1048576 1

i = deletemin(H) j = deletemin(H)

create a node numbered k with children i j

f[k] = f[i] + f[j]

insert(H k)

httpenwikipediaorgwikiHuffman_coding

Working of the Huffman Encoding Algorithm

httpenwikipediaorgwikiHuffman_coding

Complexity

we use a greedy O(nlog(n))

httpwwwsiggraphorgeducationmaterialsHyperGraphvideompegmpegfaqhuffman_tutorialhtml

Knapsack Problem

You have a knapsack that has capacity (weight) W You have several items 1 to n Each item Ij has a weight wj and a benefit bj You want to place a certain number of copies of

each item Ij in the knapsack so that The knapsack weight capacity in not exceeded the total benefits is maximal

httpenwikipediaorgwikiKnapsack_problemApplications

Knapsack Problem

httpenwikipediaorgwikiKnapsack_problemmediaFileKnapsacksvg

Knapsack Problem Variants

01 knapsack Problem The item cannot be divided

Fractional knapsack problem For instance items are liquid or powders

Solvable with a greedy algorithm

httpenwikipediaorgwikiKnapsack_problemApplications

Greedy Knapsack - Fractional

Much Easier For item Ij let ratioj=bjwj This gives you

the benefit per measure of weight Sort the items in descending order of rj If the next item cannot fit into the

knapsack break it and pick it partially just to fill the knapsack

httpenwikipediaorgwikiKnapsack_problemApplications

Greedy Knapsack Algorithm

P and W are arrays contain the profit and weight n objects ordered such that p[i]w[i]=gtp[i+1]w[1+i] that is in decreasing order m is the knapsack size and x is the solution vector

GreedyKnapsack(mn)

bull For(i=0iltni++)

bull X[i]=0

bull Int total=m

bull For (i=0 iltn i++)

bull If(w[i] lt = total)

bull X[i]= 1

bull Total= total + w[i]

bull Else

bull X[i] = total w[i]

bull end Greedyknapsack

httpjava90blogspotcom201202knapsack-problem-in-javahtml

Example - Greedy Knapsack

Item A B C D

Benefit 50 140 60 60

weight 5 20 10 12

Ratio = BW 10 7 6 5

bullExample Knapsack Capacity W = 30 and

httpwwwradfordedu~nokieclasses360greedyhtml

Solution

All of A all of B and ((30-25)10) of C (and none of D) Weight 5 + 20 + 10(510) = 30

Value 50 + 140 + 60(510) = 190 + 30 = 220

httpwwwradfordedu~nokieclasses360greedyhtml

Analysis

If the items are already sorted into descending order of piwi

Then the for-loop takes a time in O(n) Therefore the Total time including the sort is in O(n log

n)

httpjava90blogspotcom201202knapsack-problem-in-javahtml

Backtracking

Backtracking is a technique for systematically trying all paths through a state space

Backtracking search begins at the start state and pursues a path until it reaches either a goal or a ldquodead endrdquo

If it finds a goal it quits and returns the solution path

If it reaches a dead end it ldquobacktracksrdquo to the most recent node on the path

having unexamined siblings and continues down one of these branches

enwikipediaorgwikiBacktracking

Backtracking

Backtracking is an important tool for solving

constraint satisfaction problems such as

crosswords

verbal arithmetic

Sudoku

and many other puzzles

8 Queen Puzzle Algorithm is an example of Backtracking

enwikipediaorgwikiBacktracking

Backtracking

Advantages

Quick test

Pair Matching

Following real life Concept

Disadvantages

Not widely Implemented

Cannot Express Left Recursion

More Time and Complexity

httpwwwclcamacuk~mr10backtrkpdf

Eight queens puzzle

The eight queens puzzle is the problem of placing eight chess queens on an 8times8 chessboard so that no two queens threaten each other

Thus a solution requires that no two queens share the same row column or diagonal

httpenwikipediaorgwikiEight_queens_puzzle

Eight queens puzzle Algorithm

Function backtrack

Begin

SL = [Start] NSL = [Start] DE = [] CS = Start initialize

While NSL ne [ ] do while there are states to be tried

Begin

If CS = goal (or meets goal description) then return SL on success return list of states in path

If CS has no children (excluding nodes already on DE SL and NSL) then

Begin

While SL is not empty and CS = the first element of SL do

Eight queens puzzle Algorithm

Begin

Add CS to DE

Remove first element from SL

Remove first element from NSL

CS ~ first element of NSL

End

Add CS to SL

End

Eight queens puzzle Algorithm

Else begin

Place children of CS (except nodes already on DE SL or NSL) on NSL

CS = first element of NSL

Add CS to SL

End

End

Eight queens puzzle Algorithm

httpwwwcodeprojectcomArticles806248Eight-Queens-Puzzle-Tree-Algorithm

Complexity

The running time of your algorithm is at most N(Nminus1)(Nminus2)⋯(NminusK+1) ie N(NminusK) This is O(NK) ie exponential in K

httpwwwcheggcomhomework-helpquestions-and-answerspoor-man-s-n-queens-problemn-queens-arranged-n-x-n-chessboard-way-queen-checks-queen-queen-q1009394

  • Algorithm Strategies
  • Algorithms Strategy in Assignment
  • Roadmap of Every Strategy
  • Decrease and Conquer
  • Decrease and Conquer
  • Decrease and Conquer
  • Breadth-first search Algorithm
  • Algorithm of BFS
  • Order in which the nodes are expanded
  • Complexity
  • Greedy Approach
  • Uses of Greedy Approach
  • Greedy Approach (2)
  • Huffman Encoding
  • Huffman Encoding Algorithm
  • Working of the Huffman Encoding Algorithm
  • Complexity (2)
  • Knapsack Problem
  • Knapsack Problem
  • Knapsack Problem Variants
  • Greedy Knapsack - Fractional
  • Greedy Knapsack Algorithm
  • Example - Greedy Knapsack
  • Solution
  • Analysis
  • Backtracking
  • Backtracking (2)
  • Backtracking (3)
  • Eight queens puzzle
  • Eight queens puzzle Algorithm
  • Eight queens puzzle Algorithm (2)
  • Eight queens puzzle Algorithm (3)
  • Eight queens puzzle Algorithm
  • Complexity (3)
Page 16: Comparitive Analysis of Algorithm strategies

Working of the Huffman Encoding Algorithm

httpenwikipediaorgwikiHuffman_coding

Complexity

we use a greedy O(nlog(n))

httpwwwsiggraphorgeducationmaterialsHyperGraphvideompegmpegfaqhuffman_tutorialhtml

Knapsack Problem

You have a knapsack that has capacity (weight) W You have several items 1 to n Each item Ij has a weight wj and a benefit bj You want to place a certain number of copies of

each item Ij in the knapsack so that The knapsack weight capacity in not exceeded the total benefits is maximal

httpenwikipediaorgwikiKnapsack_problemApplications

Knapsack Problem

httpenwikipediaorgwikiKnapsack_problemmediaFileKnapsacksvg

Knapsack Problem Variants

01 knapsack Problem The item cannot be divided

Fractional knapsack problem For instance items are liquid or powders

Solvable with a greedy algorithm

httpenwikipediaorgwikiKnapsack_problemApplications

Greedy Knapsack - Fractional

Much Easier For item Ij let ratioj=bjwj This gives you

the benefit per measure of weight Sort the items in descending order of rj If the next item cannot fit into the

knapsack break it and pick it partially just to fill the knapsack

httpenwikipediaorgwikiKnapsack_problemApplications

Greedy Knapsack Algorithm

P and W are arrays contain the profit and weight n objects ordered such that p[i]w[i]=gtp[i+1]w[1+i] that is in decreasing order m is the knapsack size and x is the solution vector

GreedyKnapsack(mn)

bull For(i=0iltni++)

bull X[i]=0

bull Int total=m

bull For (i=0 iltn i++)

bull If(w[i] lt = total)

bull X[i]= 1

bull Total= total + w[i]

bull Else

bull X[i] = total w[i]

bull end Greedyknapsack

httpjava90blogspotcom201202knapsack-problem-in-javahtml

Example - Greedy Knapsack

Item A B C D

Benefit 50 140 60 60

weight 5 20 10 12

Ratio = BW 10 7 6 5

bullExample Knapsack Capacity W = 30 and

httpwwwradfordedu~nokieclasses360greedyhtml

Solution

All of A all of B and ((30-25)10) of C (and none of D) Weight 5 + 20 + 10(510) = 30

Value 50 + 140 + 60(510) = 190 + 30 = 220

httpwwwradfordedu~nokieclasses360greedyhtml

Analysis

If the items are already sorted into descending order of piwi

Then the for-loop takes a time in O(n) Therefore the Total time including the sort is in O(n log

n)

httpjava90blogspotcom201202knapsack-problem-in-javahtml

Backtracking

Backtracking is a technique for systematically trying all paths through a state space

Backtracking search begins at the start state and pursues a path until it reaches either a goal or a ldquodead endrdquo

If it finds a goal it quits and returns the solution path

If it reaches a dead end it ldquobacktracksrdquo to the most recent node on the path

having unexamined siblings and continues down one of these branches

enwikipediaorgwikiBacktracking

Backtracking

Backtracking is an important tool for solving

constraint satisfaction problems such as

crosswords

verbal arithmetic

Sudoku

and many other puzzles

8 Queen Puzzle Algorithm is an example of Backtracking

enwikipediaorgwikiBacktracking

Backtracking

Advantages

Quick test

Pair Matching

Following real life Concept

Disadvantages

Not widely Implemented

Cannot Express Left Recursion

More Time and Complexity

httpwwwclcamacuk~mr10backtrkpdf

Eight queens puzzle

The eight queens puzzle is the problem of placing eight chess queens on an 8times8 chessboard so that no two queens threaten each other

Thus a solution requires that no two queens share the same row column or diagonal

httpenwikipediaorgwikiEight_queens_puzzle

Eight queens puzzle Algorithm

Function backtrack

Begin

SL = [Start] NSL = [Start] DE = [] CS = Start initialize

While NSL ne [ ] do while there are states to be tried

Begin

If CS = goal (or meets goal description) then return SL on success return list of states in path

If CS has no children (excluding nodes already on DE SL and NSL) then

Begin

While SL is not empty and CS = the first element of SL do

Eight queens puzzle Algorithm

Begin

Add CS to DE

Remove first element from SL

Remove first element from NSL

CS ~ first element of NSL

End

Add CS to SL

End

Eight queens puzzle Algorithm

Else begin

Place children of CS (except nodes already on DE SL or NSL) on NSL

CS = first element of NSL

Add CS to SL

End

End

Eight queens puzzle Algorithm

httpwwwcodeprojectcomArticles806248Eight-Queens-Puzzle-Tree-Algorithm

Complexity

The running time of your algorithm is at most N(Nminus1)(Nminus2)⋯(NminusK+1) ie N(NminusK) This is O(NK) ie exponential in K

httpwwwcheggcomhomework-helpquestions-and-answerspoor-man-s-n-queens-problemn-queens-arranged-n-x-n-chessboard-way-queen-checks-queen-queen-q1009394

  • Algorithm Strategies
  • Algorithms Strategy in Assignment
  • Roadmap of Every Strategy
  • Decrease and Conquer
  • Decrease and Conquer
  • Decrease and Conquer
  • Breadth-first search Algorithm
  • Algorithm of BFS
  • Order in which the nodes are expanded
  • Complexity
  • Greedy Approach
  • Uses of Greedy Approach
  • Greedy Approach (2)
  • Huffman Encoding
  • Huffman Encoding Algorithm
  • Working of the Huffman Encoding Algorithm
  • Complexity (2)
  • Knapsack Problem
  • Knapsack Problem
  • Knapsack Problem Variants
  • Greedy Knapsack - Fractional
  • Greedy Knapsack Algorithm
  • Example - Greedy Knapsack
  • Solution
  • Analysis
  • Backtracking
  • Backtracking (2)
  • Backtracking (3)
  • Eight queens puzzle
  • Eight queens puzzle Algorithm
  • Eight queens puzzle Algorithm (2)
  • Eight queens puzzle Algorithm (3)
  • Eight queens puzzle Algorithm
  • Complexity (3)
Page 17: Comparitive Analysis of Algorithm strategies

Complexity

we use a greedy O(nlog(n))

httpwwwsiggraphorgeducationmaterialsHyperGraphvideompegmpegfaqhuffman_tutorialhtml

Knapsack Problem

You have a knapsack that has capacity (weight) W You have several items 1 to n Each item Ij has a weight wj and a benefit bj You want to place a certain number of copies of

each item Ij in the knapsack so that The knapsack weight capacity in not exceeded the total benefits is maximal

httpenwikipediaorgwikiKnapsack_problemApplications

Knapsack Problem

httpenwikipediaorgwikiKnapsack_problemmediaFileKnapsacksvg

Knapsack Problem Variants

01 knapsack Problem The item cannot be divided

Fractional knapsack problem For instance items are liquid or powders

Solvable with a greedy algorithm

httpenwikipediaorgwikiKnapsack_problemApplications

Greedy Knapsack - Fractional

Much Easier For item Ij let ratioj=bjwj This gives you

the benefit per measure of weight Sort the items in descending order of rj If the next item cannot fit into the

knapsack break it and pick it partially just to fill the knapsack

httpenwikipediaorgwikiKnapsack_problemApplications

Greedy Knapsack Algorithm

P and W are arrays contain the profit and weight n objects ordered such that p[i]w[i]=gtp[i+1]w[1+i] that is in decreasing order m is the knapsack size and x is the solution vector

GreedyKnapsack(mn)

bull For(i=0iltni++)

bull X[i]=0

bull Int total=m

bull For (i=0 iltn i++)

bull If(w[i] lt = total)

bull X[i]= 1

bull Total= total + w[i]

bull Else

bull X[i] = total w[i]

bull end Greedyknapsack

httpjava90blogspotcom201202knapsack-problem-in-javahtml

Example - Greedy Knapsack

Item A B C D

Benefit 50 140 60 60

weight 5 20 10 12

Ratio = BW 10 7 6 5

bullExample Knapsack Capacity W = 30 and

httpwwwradfordedu~nokieclasses360greedyhtml

Solution

All of A all of B and ((30-25)10) of C (and none of D) Weight 5 + 20 + 10(510) = 30

Value 50 + 140 + 60(510) = 190 + 30 = 220

httpwwwradfordedu~nokieclasses360greedyhtml

Analysis

If the items are already sorted into descending order of piwi

Then the for-loop takes a time in O(n) Therefore the Total time including the sort is in O(n log

n)

httpjava90blogspotcom201202knapsack-problem-in-javahtml

Backtracking

Backtracking is a technique for systematically trying all paths through a state space

Backtracking search begins at the start state and pursues a path until it reaches either a goal or a ldquodead endrdquo

If it finds a goal it quits and returns the solution path

If it reaches a dead end it ldquobacktracksrdquo to the most recent node on the path

having unexamined siblings and continues down one of these branches

enwikipediaorgwikiBacktracking

Backtracking

Backtracking is an important tool for solving

constraint satisfaction problems such as

crosswords

verbal arithmetic

Sudoku

and many other puzzles

8 Queen Puzzle Algorithm is an example of Backtracking

enwikipediaorgwikiBacktracking

Backtracking

Advantages

Quick test

Pair Matching

Following real life Concept

Disadvantages

Not widely Implemented

Cannot Express Left Recursion

More Time and Complexity

httpwwwclcamacuk~mr10backtrkpdf

Eight queens puzzle

The eight queens puzzle is the problem of placing eight chess queens on an 8times8 chessboard so that no two queens threaten each other

Thus a solution requires that no two queens share the same row column or diagonal

httpenwikipediaorgwikiEight_queens_puzzle

Eight queens puzzle Algorithm

Function backtrack

Begin

SL = [Start] NSL = [Start] DE = [] CS = Start initialize

While NSL ne [ ] do while there are states to be tried

Begin

If CS = goal (or meets goal description) then return SL on success return list of states in path

If CS has no children (excluding nodes already on DE SL and NSL) then

Begin

While SL is not empty and CS = the first element of SL do

Eight queens puzzle Algorithm

Begin

Add CS to DE

Remove first element from SL

Remove first element from NSL

CS ~ first element of NSL

End

Add CS to SL

End

Eight queens puzzle Algorithm

Else begin

Place children of CS (except nodes already on DE SL or NSL) on NSL

CS = first element of NSL

Add CS to SL

End

End

Eight queens puzzle Algorithm

httpwwwcodeprojectcomArticles806248Eight-Queens-Puzzle-Tree-Algorithm

Complexity

The running time of your algorithm is at most N(Nminus1)(Nminus2)⋯(NminusK+1) ie N(NminusK) This is O(NK) ie exponential in K

httpwwwcheggcomhomework-helpquestions-and-answerspoor-man-s-n-queens-problemn-queens-arranged-n-x-n-chessboard-way-queen-checks-queen-queen-q1009394

  • Algorithm Strategies
  • Algorithms Strategy in Assignment
  • Roadmap of Every Strategy
  • Decrease and Conquer
  • Decrease and Conquer
  • Decrease and Conquer
  • Breadth-first search Algorithm
  • Algorithm of BFS
  • Order in which the nodes are expanded
  • Complexity
  • Greedy Approach
  • Uses of Greedy Approach
  • Greedy Approach (2)
  • Huffman Encoding
  • Huffman Encoding Algorithm
  • Working of the Huffman Encoding Algorithm
  • Complexity (2)
  • Knapsack Problem
  • Knapsack Problem
  • Knapsack Problem Variants
  • Greedy Knapsack - Fractional
  • Greedy Knapsack Algorithm
  • Example - Greedy Knapsack
  • Solution
  • Analysis
  • Backtracking
  • Backtracking (2)
  • Backtracking (3)
  • Eight queens puzzle
  • Eight queens puzzle Algorithm
  • Eight queens puzzle Algorithm (2)
  • Eight queens puzzle Algorithm (3)
  • Eight queens puzzle Algorithm
  • Complexity (3)
Page 18: Comparitive Analysis of Algorithm strategies

Knapsack Problem

You have a knapsack that has capacity (weight) W You have several items 1 to n Each item Ij has a weight wj and a benefit bj You want to place a certain number of copies of

each item Ij in the knapsack so that The knapsack weight capacity in not exceeded the total benefits is maximal

httpenwikipediaorgwikiKnapsack_problemApplications

Knapsack Problem

httpenwikipediaorgwikiKnapsack_problemmediaFileKnapsacksvg

Knapsack Problem Variants

01 knapsack Problem The item cannot be divided

Fractional knapsack problem For instance items are liquid or powders

Solvable with a greedy algorithm

httpenwikipediaorgwikiKnapsack_problemApplications

Greedy Knapsack - Fractional

Much Easier For item Ij let ratioj=bjwj This gives you

the benefit per measure of weight Sort the items in descending order of rj If the next item cannot fit into the

knapsack break it and pick it partially just to fill the knapsack

httpenwikipediaorgwikiKnapsack_problemApplications

Greedy Knapsack Algorithm

P and W are arrays contain the profit and weight n objects ordered such that p[i]w[i]=gtp[i+1]w[1+i] that is in decreasing order m is the knapsack size and x is the solution vector

GreedyKnapsack(mn)

bull For(i=0iltni++)

bull X[i]=0

bull Int total=m

bull For (i=0 iltn i++)

bull If(w[i] lt = total)

bull X[i]= 1

bull Total= total + w[i]

bull Else

bull X[i] = total w[i]

bull end Greedyknapsack

httpjava90blogspotcom201202knapsack-problem-in-javahtml

Example - Greedy Knapsack

Item A B C D

Benefit 50 140 60 60

weight 5 20 10 12

Ratio = BW 10 7 6 5

bullExample Knapsack Capacity W = 30 and

httpwwwradfordedu~nokieclasses360greedyhtml

Solution

All of A all of B and ((30-25)10) of C (and none of D) Weight 5 + 20 + 10(510) = 30

Value 50 + 140 + 60(510) = 190 + 30 = 220

httpwwwradfordedu~nokieclasses360greedyhtml

Analysis

If the items are already sorted into descending order of piwi

Then the for-loop takes a time in O(n) Therefore the Total time including the sort is in O(n log

n)

httpjava90blogspotcom201202knapsack-problem-in-javahtml

Backtracking

Backtracking is a technique for systematically trying all paths through a state space

Backtracking search begins at the start state and pursues a path until it reaches either a goal or a ldquodead endrdquo

If it finds a goal it quits and returns the solution path

If it reaches a dead end it ldquobacktracksrdquo to the most recent node on the path

having unexamined siblings and continues down one of these branches

enwikipediaorgwikiBacktracking

Backtracking

Backtracking is an important tool for solving

constraint satisfaction problems such as

crosswords

verbal arithmetic

Sudoku

and many other puzzles

8 Queen Puzzle Algorithm is an example of Backtracking

enwikipediaorgwikiBacktracking

Backtracking

Advantages

Quick test

Pair Matching

Following real life Concept

Disadvantages

Not widely Implemented

Cannot Express Left Recursion

More Time and Complexity

httpwwwclcamacuk~mr10backtrkpdf

Eight queens puzzle

The eight queens puzzle is the problem of placing eight chess queens on an 8times8 chessboard so that no two queens threaten each other

Thus a solution requires that no two queens share the same row column or diagonal

httpenwikipediaorgwikiEight_queens_puzzle

Eight queens puzzle Algorithm

Function backtrack

Begin

SL = [Start] NSL = [Start] DE = [] CS = Start initialize

While NSL ne [ ] do while there are states to be tried

Begin

If CS = goal (or meets goal description) then return SL on success return list of states in path

If CS has no children (excluding nodes already on DE SL and NSL) then

Begin

While SL is not empty and CS = the first element of SL do

Eight queens puzzle Algorithm

Begin

Add CS to DE

Remove first element from SL

Remove first element from NSL

CS ~ first element of NSL

End

Add CS to SL

End

Eight queens puzzle Algorithm

Else begin

Place children of CS (except nodes already on DE SL or NSL) on NSL

CS = first element of NSL

Add CS to SL

End

End

Eight queens puzzle Algorithm

httpwwwcodeprojectcomArticles806248Eight-Queens-Puzzle-Tree-Algorithm

Complexity

The running time of your algorithm is at most N(Nminus1)(Nminus2)⋯(NminusK+1) ie N(NminusK) This is O(NK) ie exponential in K

httpwwwcheggcomhomework-helpquestions-and-answerspoor-man-s-n-queens-problemn-queens-arranged-n-x-n-chessboard-way-queen-checks-queen-queen-q1009394

  • Algorithm Strategies
  • Algorithms Strategy in Assignment
  • Roadmap of Every Strategy
  • Decrease and Conquer
  • Decrease and Conquer
  • Decrease and Conquer
  • Breadth-first search Algorithm
  • Algorithm of BFS
  • Order in which the nodes are expanded
  • Complexity
  • Greedy Approach
  • Uses of Greedy Approach
  • Greedy Approach (2)
  • Huffman Encoding
  • Huffman Encoding Algorithm
  • Working of the Huffman Encoding Algorithm
  • Complexity (2)
  • Knapsack Problem
  • Knapsack Problem
  • Knapsack Problem Variants
  • Greedy Knapsack - Fractional
  • Greedy Knapsack Algorithm
  • Example - Greedy Knapsack
  • Solution
  • Analysis
  • Backtracking
  • Backtracking (2)
  • Backtracking (3)
  • Eight queens puzzle
  • Eight queens puzzle Algorithm
  • Eight queens puzzle Algorithm (2)
  • Eight queens puzzle Algorithm (3)
  • Eight queens puzzle Algorithm
  • Complexity (3)
Page 19: Comparitive Analysis of Algorithm strategies

Knapsack Problem

httpenwikipediaorgwikiKnapsack_problemmediaFileKnapsacksvg

Knapsack Problem Variants

01 knapsack Problem The item cannot be divided

Fractional knapsack problem For instance items are liquid or powders

Solvable with a greedy algorithm

httpenwikipediaorgwikiKnapsack_problemApplications

Greedy Knapsack - Fractional

Much Easier For item Ij let ratioj=bjwj This gives you

the benefit per measure of weight Sort the items in descending order of rj If the next item cannot fit into the

knapsack break it and pick it partially just to fill the knapsack

httpenwikipediaorgwikiKnapsack_problemApplications

Greedy Knapsack Algorithm

P and W are arrays contain the profit and weight n objects ordered such that p[i]w[i]=gtp[i+1]w[1+i] that is in decreasing order m is the knapsack size and x is the solution vector

GreedyKnapsack(mn)

bull For(i=0iltni++)

bull X[i]=0

bull Int total=m

bull For (i=0 iltn i++)

bull If(w[i] lt = total)

bull X[i]= 1

bull Total= total + w[i]

bull Else

bull X[i] = total w[i]

bull end Greedyknapsack

httpjava90blogspotcom201202knapsack-problem-in-javahtml

Example - Greedy Knapsack

Item A B C D

Benefit 50 140 60 60

weight 5 20 10 12

Ratio = BW 10 7 6 5

bullExample Knapsack Capacity W = 30 and

httpwwwradfordedu~nokieclasses360greedyhtml

Solution

All of A all of B and ((30-25)10) of C (and none of D) Weight 5 + 20 + 10(510) = 30

Value 50 + 140 + 60(510) = 190 + 30 = 220

httpwwwradfordedu~nokieclasses360greedyhtml

Analysis

If the items are already sorted into descending order of piwi

Then the for-loop takes a time in O(n) Therefore the Total time including the sort is in O(n log

n)

httpjava90blogspotcom201202knapsack-problem-in-javahtml

Backtracking

Backtracking is a technique for systematically trying all paths through a state space

Backtracking search begins at the start state and pursues a path until it reaches either a goal or a ldquodead endrdquo

If it finds a goal it quits and returns the solution path

If it reaches a dead end it ldquobacktracksrdquo to the most recent node on the path

having unexamined siblings and continues down one of these branches

enwikipediaorgwikiBacktracking

Backtracking

Backtracking is an important tool for solving

constraint satisfaction problems such as

crosswords

verbal arithmetic

Sudoku

and many other puzzles

8 Queen Puzzle Algorithm is an example of Backtracking

enwikipediaorgwikiBacktracking

Backtracking

Advantages

Quick test

Pair Matching

Following real life Concept

Disadvantages

Not widely Implemented

Cannot Express Left Recursion

More Time and Complexity

httpwwwclcamacuk~mr10backtrkpdf

Eight queens puzzle

The eight queens puzzle is the problem of placing eight chess queens on an 8times8 chessboard so that no two queens threaten each other

Thus a solution requires that no two queens share the same row column or diagonal

httpenwikipediaorgwikiEight_queens_puzzle

Eight queens puzzle Algorithm

Function backtrack

Begin

SL = [Start] NSL = [Start] DE = [] CS = Start initialize

While NSL ne [ ] do while there are states to be tried

Begin

If CS = goal (or meets goal description) then return SL on success return list of states in path

If CS has no children (excluding nodes already on DE SL and NSL) then

Begin

While SL is not empty and CS = the first element of SL do

Eight queens puzzle Algorithm

Begin

Add CS to DE

Remove first element from SL

Remove first element from NSL

CS ~ first element of NSL

End

Add CS to SL

End

Eight queens puzzle Algorithm

Else begin

Place children of CS (except nodes already on DE SL or NSL) on NSL

CS = first element of NSL

Add CS to SL

End

End

Eight queens puzzle Algorithm

httpwwwcodeprojectcomArticles806248Eight-Queens-Puzzle-Tree-Algorithm

Complexity

The running time of your algorithm is at most N(Nminus1)(Nminus2)⋯(NminusK+1) ie N(NminusK) This is O(NK) ie exponential in K

httpwwwcheggcomhomework-helpquestions-and-answerspoor-man-s-n-queens-problemn-queens-arranged-n-x-n-chessboard-way-queen-checks-queen-queen-q1009394

  • Algorithm Strategies
  • Algorithms Strategy in Assignment
  • Roadmap of Every Strategy
  • Decrease and Conquer
  • Decrease and Conquer
  • Decrease and Conquer
  • Breadth-first search Algorithm
  • Algorithm of BFS
  • Order in which the nodes are expanded
  • Complexity
  • Greedy Approach
  • Uses of Greedy Approach
  • Greedy Approach (2)
  • Huffman Encoding
  • Huffman Encoding Algorithm
  • Working of the Huffman Encoding Algorithm
  • Complexity (2)
  • Knapsack Problem
  • Knapsack Problem
  • Knapsack Problem Variants
  • Greedy Knapsack - Fractional
  • Greedy Knapsack Algorithm
  • Example - Greedy Knapsack
  • Solution
  • Analysis
  • Backtracking
  • Backtracking (2)
  • Backtracking (3)
  • Eight queens puzzle
  • Eight queens puzzle Algorithm
  • Eight queens puzzle Algorithm (2)
  • Eight queens puzzle Algorithm (3)
  • Eight queens puzzle Algorithm
  • Complexity (3)
Page 20: Comparitive Analysis of Algorithm strategies

Knapsack Problem Variants

01 knapsack Problem The item cannot be divided

Fractional knapsack problem For instance items are liquid or powders

Solvable with a greedy algorithm

httpenwikipediaorgwikiKnapsack_problemApplications

Greedy Knapsack - Fractional

Much Easier For item Ij let ratioj=bjwj This gives you

the benefit per measure of weight Sort the items in descending order of rj If the next item cannot fit into the

knapsack break it and pick it partially just to fill the knapsack

httpenwikipediaorgwikiKnapsack_problemApplications

Greedy Knapsack Algorithm

P and W are arrays contain the profit and weight n objects ordered such that p[i]w[i]=gtp[i+1]w[1+i] that is in decreasing order m is the knapsack size and x is the solution vector

GreedyKnapsack(mn)

bull For(i=0iltni++)

bull X[i]=0

bull Int total=m

bull For (i=0 iltn i++)

bull If(w[i] lt = total)

bull X[i]= 1

bull Total= total + w[i]

bull Else

bull X[i] = total w[i]

bull end Greedyknapsack

httpjava90blogspotcom201202knapsack-problem-in-javahtml

Example - Greedy Knapsack

Item A B C D

Benefit 50 140 60 60

weight 5 20 10 12

Ratio = BW 10 7 6 5

bullExample Knapsack Capacity W = 30 and

httpwwwradfordedu~nokieclasses360greedyhtml

Solution

All of A all of B and ((30-25)10) of C (and none of D) Weight 5 + 20 + 10(510) = 30

Value 50 + 140 + 60(510) = 190 + 30 = 220

httpwwwradfordedu~nokieclasses360greedyhtml

Analysis

If the items are already sorted into descending order of piwi

Then the for-loop takes a time in O(n) Therefore the Total time including the sort is in O(n log

n)

httpjava90blogspotcom201202knapsack-problem-in-javahtml

Backtracking

Backtracking is a technique for systematically trying all paths through a state space

Backtracking search begins at the start state and pursues a path until it reaches either a goal or a ldquodead endrdquo

If it finds a goal it quits and returns the solution path

If it reaches a dead end it ldquobacktracksrdquo to the most recent node on the path

having unexamined siblings and continues down one of these branches

enwikipediaorgwikiBacktracking

Backtracking

Backtracking is an important tool for solving

constraint satisfaction problems such as

crosswords

verbal arithmetic

Sudoku

and many other puzzles

8 Queen Puzzle Algorithm is an example of Backtracking

enwikipediaorgwikiBacktracking

Backtracking

Advantages

Quick test

Pair Matching

Following real life Concept

Disadvantages

Not widely Implemented

Cannot Express Left Recursion

More Time and Complexity

httpwwwclcamacuk~mr10backtrkpdf

Eight queens puzzle

The eight queens puzzle is the problem of placing eight chess queens on an 8times8 chessboard so that no two queens threaten each other

Thus a solution requires that no two queens share the same row column or diagonal

httpenwikipediaorgwikiEight_queens_puzzle

Eight queens puzzle Algorithm

Function backtrack

Begin

SL = [Start] NSL = [Start] DE = [] CS = Start initialize

While NSL ne [ ] do while there are states to be tried

Begin

If CS = goal (or meets goal description) then return SL on success return list of states in path

If CS has no children (excluding nodes already on DE SL and NSL) then

Begin

While SL is not empty and CS = the first element of SL do

Eight queens puzzle Algorithm

Begin

Add CS to DE

Remove first element from SL

Remove first element from NSL

CS ~ first element of NSL

End

Add CS to SL

End

Eight queens puzzle Algorithm

Else begin

Place children of CS (except nodes already on DE SL or NSL) on NSL

CS = first element of NSL

Add CS to SL

End

End

Eight queens puzzle Algorithm

httpwwwcodeprojectcomArticles806248Eight-Queens-Puzzle-Tree-Algorithm

Complexity

The running time of your algorithm is at most N(Nminus1)(Nminus2)⋯(NminusK+1) ie N(NminusK) This is O(NK) ie exponential in K

httpwwwcheggcomhomework-helpquestions-and-answerspoor-man-s-n-queens-problemn-queens-arranged-n-x-n-chessboard-way-queen-checks-queen-queen-q1009394

  • Algorithm Strategies
  • Algorithms Strategy in Assignment
  • Roadmap of Every Strategy
  • Decrease and Conquer
  • Decrease and Conquer
  • Decrease and Conquer
  • Breadth-first search Algorithm
  • Algorithm of BFS
  • Order in which the nodes are expanded
  • Complexity
  • Greedy Approach
  • Uses of Greedy Approach
  • Greedy Approach (2)
  • Huffman Encoding
  • Huffman Encoding Algorithm
  • Working of the Huffman Encoding Algorithm
  • Complexity (2)
  • Knapsack Problem
  • Knapsack Problem
  • Knapsack Problem Variants
  • Greedy Knapsack - Fractional
  • Greedy Knapsack Algorithm
  • Example - Greedy Knapsack
  • Solution
  • Analysis
  • Backtracking
  • Backtracking (2)
  • Backtracking (3)
  • Eight queens puzzle
  • Eight queens puzzle Algorithm
  • Eight queens puzzle Algorithm (2)
  • Eight queens puzzle Algorithm (3)
  • Eight queens puzzle Algorithm
  • Complexity (3)
Page 21: Comparitive Analysis of Algorithm strategies

Greedy Knapsack - Fractional

Much Easier For item Ij let ratioj=bjwj This gives you

the benefit per measure of weight Sort the items in descending order of rj If the next item cannot fit into the

knapsack break it and pick it partially just to fill the knapsack

httpenwikipediaorgwikiKnapsack_problemApplications

Greedy Knapsack Algorithm

P and W are arrays contain the profit and weight n objects ordered such that p[i]w[i]=gtp[i+1]w[1+i] that is in decreasing order m is the knapsack size and x is the solution vector

GreedyKnapsack(mn)

bull For(i=0iltni++)

bull X[i]=0

bull Int total=m

bull For (i=0 iltn i++)

bull If(w[i] lt = total)

bull X[i]= 1

bull Total= total + w[i]

bull Else

bull X[i] = total w[i]

bull end Greedyknapsack

httpjava90blogspotcom201202knapsack-problem-in-javahtml

Example - Greedy Knapsack

Item A B C D

Benefit 50 140 60 60

weight 5 20 10 12

Ratio = BW 10 7 6 5

bullExample Knapsack Capacity W = 30 and

httpwwwradfordedu~nokieclasses360greedyhtml

Solution

All of A all of B and ((30-25)10) of C (and none of D) Weight 5 + 20 + 10(510) = 30

Value 50 + 140 + 60(510) = 190 + 30 = 220

httpwwwradfordedu~nokieclasses360greedyhtml

Analysis

If the items are already sorted into descending order of piwi

Then the for-loop takes a time in O(n) Therefore the Total time including the sort is in O(n log

n)

httpjava90blogspotcom201202knapsack-problem-in-javahtml

Backtracking

Backtracking is a technique for systematically trying all paths through a state space

Backtracking search begins at the start state and pursues a path until it reaches either a goal or a ldquodead endrdquo

If it finds a goal it quits and returns the solution path

If it reaches a dead end it ldquobacktracksrdquo to the most recent node on the path

having unexamined siblings and continues down one of these branches

enwikipediaorgwikiBacktracking

Backtracking

Backtracking is an important tool for solving

constraint satisfaction problems such as

crosswords

verbal arithmetic

Sudoku

and many other puzzles

8 Queen Puzzle Algorithm is an example of Backtracking

enwikipediaorgwikiBacktracking

Backtracking

Advantages

Quick test

Pair Matching

Following real life Concept

Disadvantages

Not widely Implemented

Cannot Express Left Recursion

More Time and Complexity

httpwwwclcamacuk~mr10backtrkpdf

Eight queens puzzle

The eight queens puzzle is the problem of placing eight chess queens on an 8times8 chessboard so that no two queens threaten each other

Thus a solution requires that no two queens share the same row column or diagonal

httpenwikipediaorgwikiEight_queens_puzzle

Eight queens puzzle Algorithm

Function backtrack

Begin

SL = [Start] NSL = [Start] DE = [] CS = Start initialize

While NSL ne [ ] do while there are states to be tried

Begin

If CS = goal (or meets goal description) then return SL on success return list of states in path

If CS has no children (excluding nodes already on DE SL and NSL) then

Begin

While SL is not empty and CS = the first element of SL do

Eight queens puzzle Algorithm

Begin

Add CS to DE

Remove first element from SL

Remove first element from NSL

CS ~ first element of NSL

End

Add CS to SL

End

Eight queens puzzle Algorithm

Else begin

Place children of CS (except nodes already on DE SL or NSL) on NSL

CS = first element of NSL

Add CS to SL

End

End

Eight queens puzzle Algorithm

httpwwwcodeprojectcomArticles806248Eight-Queens-Puzzle-Tree-Algorithm

Complexity

The running time of your algorithm is at most N(Nminus1)(Nminus2)⋯(NminusK+1) ie N(NminusK) This is O(NK) ie exponential in K

httpwwwcheggcomhomework-helpquestions-and-answerspoor-man-s-n-queens-problemn-queens-arranged-n-x-n-chessboard-way-queen-checks-queen-queen-q1009394

  • Algorithm Strategies
  • Algorithms Strategy in Assignment
  • Roadmap of Every Strategy
  • Decrease and Conquer
  • Decrease and Conquer
  • Decrease and Conquer
  • Breadth-first search Algorithm
  • Algorithm of BFS
  • Order in which the nodes are expanded
  • Complexity
  • Greedy Approach
  • Uses of Greedy Approach
  • Greedy Approach (2)
  • Huffman Encoding
  • Huffman Encoding Algorithm
  • Working of the Huffman Encoding Algorithm
  • Complexity (2)
  • Knapsack Problem
  • Knapsack Problem
  • Knapsack Problem Variants
  • Greedy Knapsack - Fractional
  • Greedy Knapsack Algorithm
  • Example - Greedy Knapsack
  • Solution
  • Analysis
  • Backtracking
  • Backtracking (2)
  • Backtracking (3)
  • Eight queens puzzle
  • Eight queens puzzle Algorithm
  • Eight queens puzzle Algorithm (2)
  • Eight queens puzzle Algorithm (3)
  • Eight queens puzzle Algorithm
  • Complexity (3)
Page 22: Comparitive Analysis of Algorithm strategies

Greedy Knapsack Algorithm

P and W are arrays contain the profit and weight n objects ordered such that p[i]w[i]=gtp[i+1]w[1+i] that is in decreasing order m is the knapsack size and x is the solution vector

GreedyKnapsack(mn)

bull For(i=0iltni++)

bull X[i]=0

bull Int total=m

bull For (i=0 iltn i++)

bull If(w[i] lt = total)

bull X[i]= 1

bull Total= total + w[i]

bull Else

bull X[i] = total w[i]

bull end Greedyknapsack

httpjava90blogspotcom201202knapsack-problem-in-javahtml

Example - Greedy Knapsack

Item A B C D

Benefit 50 140 60 60

weight 5 20 10 12

Ratio = BW 10 7 6 5

bullExample Knapsack Capacity W = 30 and

httpwwwradfordedu~nokieclasses360greedyhtml

Solution

All of A all of B and ((30-25)10) of C (and none of D) Weight 5 + 20 + 10(510) = 30

Value 50 + 140 + 60(510) = 190 + 30 = 220

httpwwwradfordedu~nokieclasses360greedyhtml

Analysis

If the items are already sorted into descending order of piwi

Then the for-loop takes a time in O(n) Therefore the Total time including the sort is in O(n log

n)

httpjava90blogspotcom201202knapsack-problem-in-javahtml

Backtracking

Backtracking is a technique for systematically trying all paths through a state space

Backtracking search begins at the start state and pursues a path until it reaches either a goal or a ldquodead endrdquo

If it finds a goal it quits and returns the solution path

If it reaches a dead end it ldquobacktracksrdquo to the most recent node on the path

having unexamined siblings and continues down one of these branches

enwikipediaorgwikiBacktracking

Backtracking

Backtracking is an important tool for solving

constraint satisfaction problems such as

crosswords

verbal arithmetic

Sudoku

and many other puzzles

8 Queen Puzzle Algorithm is an example of Backtracking

enwikipediaorgwikiBacktracking

Backtracking

Advantages

Quick test

Pair Matching

Following real life Concept

Disadvantages

Not widely Implemented

Cannot Express Left Recursion

More Time and Complexity

httpwwwclcamacuk~mr10backtrkpdf

Eight queens puzzle

The eight queens puzzle is the problem of placing eight chess queens on an 8times8 chessboard so that no two queens threaten each other

Thus a solution requires that no two queens share the same row column or diagonal

httpenwikipediaorgwikiEight_queens_puzzle

Eight queens puzzle Algorithm

Function backtrack

Begin

SL = [Start] NSL = [Start] DE = [] CS = Start initialize

While NSL ne [ ] do while there are states to be tried

Begin

If CS = goal (or meets goal description) then return SL on success return list of states in path

If CS has no children (excluding nodes already on DE SL and NSL) then

Begin

While SL is not empty and CS = the first element of SL do

Eight queens puzzle Algorithm

Begin

Add CS to DE

Remove first element from SL

Remove first element from NSL

CS ~ first element of NSL

End

Add CS to SL

End

Eight queens puzzle Algorithm

Else begin

Place children of CS (except nodes already on DE SL or NSL) on NSL

CS = first element of NSL

Add CS to SL

End

End

Eight queens puzzle Algorithm

httpwwwcodeprojectcomArticles806248Eight-Queens-Puzzle-Tree-Algorithm

Complexity

The running time of your algorithm is at most N(Nminus1)(Nminus2)⋯(NminusK+1) ie N(NminusK) This is O(NK) ie exponential in K

httpwwwcheggcomhomework-helpquestions-and-answerspoor-man-s-n-queens-problemn-queens-arranged-n-x-n-chessboard-way-queen-checks-queen-queen-q1009394

  • Algorithm Strategies
  • Algorithms Strategy in Assignment
  • Roadmap of Every Strategy
  • Decrease and Conquer
  • Decrease and Conquer
  • Decrease and Conquer
  • Breadth-first search Algorithm
  • Algorithm of BFS
  • Order in which the nodes are expanded
  • Complexity
  • Greedy Approach
  • Uses of Greedy Approach
  • Greedy Approach (2)
  • Huffman Encoding
  • Huffman Encoding Algorithm
  • Working of the Huffman Encoding Algorithm
  • Complexity (2)
  • Knapsack Problem
  • Knapsack Problem
  • Knapsack Problem Variants
  • Greedy Knapsack - Fractional
  • Greedy Knapsack Algorithm
  • Example - Greedy Knapsack
  • Solution
  • Analysis
  • Backtracking
  • Backtracking (2)
  • Backtracking (3)
  • Eight queens puzzle
  • Eight queens puzzle Algorithm
  • Eight queens puzzle Algorithm (2)
  • Eight queens puzzle Algorithm (3)
  • Eight queens puzzle Algorithm
  • Complexity (3)
Page 23: Comparitive Analysis of Algorithm strategies

Example - Greedy Knapsack

Item A B C D

Benefit 50 140 60 60

weight 5 20 10 12

Ratio = BW 10 7 6 5

bullExample Knapsack Capacity W = 30 and

httpwwwradfordedu~nokieclasses360greedyhtml

Solution

All of A all of B and ((30-25)10) of C (and none of D) Weight 5 + 20 + 10(510) = 30

Value 50 + 140 + 60(510) = 190 + 30 = 220

httpwwwradfordedu~nokieclasses360greedyhtml

Analysis

If the items are already sorted into descending order of piwi

Then the for-loop takes a time in O(n) Therefore the Total time including the sort is in O(n log

n)

httpjava90blogspotcom201202knapsack-problem-in-javahtml

Backtracking

Backtracking is a technique for systematically trying all paths through a state space

Backtracking search begins at the start state and pursues a path until it reaches either a goal or a ldquodead endrdquo

If it finds a goal it quits and returns the solution path

If it reaches a dead end it ldquobacktracksrdquo to the most recent node on the path

having unexamined siblings and continues down one of these branches

enwikipediaorgwikiBacktracking

Backtracking

Backtracking is an important tool for solving

constraint satisfaction problems such as

crosswords

verbal arithmetic

Sudoku

and many other puzzles

8 Queen Puzzle Algorithm is an example of Backtracking

enwikipediaorgwikiBacktracking

Backtracking

Advantages

Quick test

Pair Matching

Following real life Concept

Disadvantages

Not widely Implemented

Cannot Express Left Recursion

More Time and Complexity

httpwwwclcamacuk~mr10backtrkpdf

Eight queens puzzle

The eight queens puzzle is the problem of placing eight chess queens on an 8times8 chessboard so that no two queens threaten each other

Thus a solution requires that no two queens share the same row column or diagonal

httpenwikipediaorgwikiEight_queens_puzzle

Eight queens puzzle Algorithm

Function backtrack

Begin

SL = [Start] NSL = [Start] DE = [] CS = Start initialize

While NSL ne [ ] do while there are states to be tried

Begin

If CS = goal (or meets goal description) then return SL on success return list of states in path

If CS has no children (excluding nodes already on DE SL and NSL) then

Begin

While SL is not empty and CS = the first element of SL do

Eight queens puzzle Algorithm

Begin

Add CS to DE

Remove first element from SL

Remove first element from NSL

CS ~ first element of NSL

End

Add CS to SL

End

Eight queens puzzle Algorithm

Else begin

Place children of CS (except nodes already on DE SL or NSL) on NSL

CS = first element of NSL

Add CS to SL

End

End

Eight queens puzzle Algorithm

httpwwwcodeprojectcomArticles806248Eight-Queens-Puzzle-Tree-Algorithm

Complexity

The running time of your algorithm is at most N(Nminus1)(Nminus2)⋯(NminusK+1) ie N(NminusK) This is O(NK) ie exponential in K

httpwwwcheggcomhomework-helpquestions-and-answerspoor-man-s-n-queens-problemn-queens-arranged-n-x-n-chessboard-way-queen-checks-queen-queen-q1009394

  • Algorithm Strategies
  • Algorithms Strategy in Assignment
  • Roadmap of Every Strategy
  • Decrease and Conquer
  • Decrease and Conquer
  • Decrease and Conquer
  • Breadth-first search Algorithm
  • Algorithm of BFS
  • Order in which the nodes are expanded
  • Complexity
  • Greedy Approach
  • Uses of Greedy Approach
  • Greedy Approach (2)
  • Huffman Encoding
  • Huffman Encoding Algorithm
  • Working of the Huffman Encoding Algorithm
  • Complexity (2)
  • Knapsack Problem
  • Knapsack Problem
  • Knapsack Problem Variants
  • Greedy Knapsack - Fractional
  • Greedy Knapsack Algorithm
  • Example - Greedy Knapsack
  • Solution
  • Analysis
  • Backtracking
  • Backtracking (2)
  • Backtracking (3)
  • Eight queens puzzle
  • Eight queens puzzle Algorithm
  • Eight queens puzzle Algorithm (2)
  • Eight queens puzzle Algorithm (3)
  • Eight queens puzzle Algorithm
  • Complexity (3)
Page 24: Comparitive Analysis of Algorithm strategies

Solution

All of A all of B and ((30-25)10) of C (and none of D) Weight 5 + 20 + 10(510) = 30

Value 50 + 140 + 60(510) = 190 + 30 = 220

httpwwwradfordedu~nokieclasses360greedyhtml

Analysis

If the items are already sorted into descending order of piwi

Then the for-loop takes a time in O(n) Therefore the Total time including the sort is in O(n log

n)

httpjava90blogspotcom201202knapsack-problem-in-javahtml

Backtracking

Backtracking is a technique for systematically trying all paths through a state space

Backtracking search begins at the start state and pursues a path until it reaches either a goal or a ldquodead endrdquo

If it finds a goal it quits and returns the solution path

If it reaches a dead end it ldquobacktracksrdquo to the most recent node on the path

having unexamined siblings and continues down one of these branches

enwikipediaorgwikiBacktracking

Backtracking

Backtracking is an important tool for solving

constraint satisfaction problems such as

crosswords

verbal arithmetic

Sudoku

and many other puzzles

8 Queen Puzzle Algorithm is an example of Backtracking

enwikipediaorgwikiBacktracking

Backtracking

Advantages

Quick test

Pair Matching

Following real life Concept

Disadvantages

Not widely Implemented

Cannot Express Left Recursion

More Time and Complexity

httpwwwclcamacuk~mr10backtrkpdf

Eight queens puzzle

The eight queens puzzle is the problem of placing eight chess queens on an 8times8 chessboard so that no two queens threaten each other

Thus a solution requires that no two queens share the same row column or diagonal

httpenwikipediaorgwikiEight_queens_puzzle

Eight queens puzzle Algorithm

Function backtrack

Begin

SL = [Start] NSL = [Start] DE = [] CS = Start initialize

While NSL ne [ ] do while there are states to be tried

Begin

If CS = goal (or meets goal description) then return SL on success return list of states in path

If CS has no children (excluding nodes already on DE SL and NSL) then

Begin

While SL is not empty and CS = the first element of SL do

Eight queens puzzle Algorithm

Begin

Add CS to DE

Remove first element from SL

Remove first element from NSL

CS ~ first element of NSL

End

Add CS to SL

End

Eight queens puzzle Algorithm

Else begin

Place children of CS (except nodes already on DE SL or NSL) on NSL

CS = first element of NSL

Add CS to SL

End

End

Eight queens puzzle Algorithm

httpwwwcodeprojectcomArticles806248Eight-Queens-Puzzle-Tree-Algorithm

Complexity

The running time of your algorithm is at most N(Nminus1)(Nminus2)⋯(NminusK+1) ie N(NminusK) This is O(NK) ie exponential in K

httpwwwcheggcomhomework-helpquestions-and-answerspoor-man-s-n-queens-problemn-queens-arranged-n-x-n-chessboard-way-queen-checks-queen-queen-q1009394

  • Algorithm Strategies
  • Algorithms Strategy in Assignment
  • Roadmap of Every Strategy
  • Decrease and Conquer
  • Decrease and Conquer
  • Decrease and Conquer
  • Breadth-first search Algorithm
  • Algorithm of BFS
  • Order in which the nodes are expanded
  • Complexity
  • Greedy Approach
  • Uses of Greedy Approach
  • Greedy Approach (2)
  • Huffman Encoding
  • Huffman Encoding Algorithm
  • Working of the Huffman Encoding Algorithm
  • Complexity (2)
  • Knapsack Problem
  • Knapsack Problem
  • Knapsack Problem Variants
  • Greedy Knapsack - Fractional
  • Greedy Knapsack Algorithm
  • Example - Greedy Knapsack
  • Solution
  • Analysis
  • Backtracking
  • Backtracking (2)
  • Backtracking (3)
  • Eight queens puzzle
  • Eight queens puzzle Algorithm
  • Eight queens puzzle Algorithm (2)
  • Eight queens puzzle Algorithm (3)
  • Eight queens puzzle Algorithm
  • Complexity (3)
Page 25: Comparitive Analysis of Algorithm strategies

Analysis

If the items are already sorted into descending order of piwi

Then the for-loop takes a time in O(n) Therefore the Total time including the sort is in O(n log

n)

httpjava90blogspotcom201202knapsack-problem-in-javahtml

Backtracking

Backtracking is a technique for systematically trying all paths through a state space

Backtracking search begins at the start state and pursues a path until it reaches either a goal or a ldquodead endrdquo

If it finds a goal it quits and returns the solution path

If it reaches a dead end it ldquobacktracksrdquo to the most recent node on the path

having unexamined siblings and continues down one of these branches

enwikipediaorgwikiBacktracking

Backtracking

Backtracking is an important tool for solving

constraint satisfaction problems such as

crosswords

verbal arithmetic

Sudoku

and many other puzzles

8 Queen Puzzle Algorithm is an example of Backtracking

enwikipediaorgwikiBacktracking

Backtracking

Advantages

Quick test

Pair Matching

Following real life Concept

Disadvantages

Not widely Implemented

Cannot Express Left Recursion

More Time and Complexity

httpwwwclcamacuk~mr10backtrkpdf

Eight queens puzzle

The eight queens puzzle is the problem of placing eight chess queens on an 8times8 chessboard so that no two queens threaten each other

Thus a solution requires that no two queens share the same row column or diagonal

httpenwikipediaorgwikiEight_queens_puzzle

Eight queens puzzle Algorithm

Function backtrack

Begin

SL = [Start] NSL = [Start] DE = [] CS = Start initialize

While NSL ne [ ] do while there are states to be tried

Begin

If CS = goal (or meets goal description) then return SL on success return list of states in path

If CS has no children (excluding nodes already on DE SL and NSL) then

Begin

While SL is not empty and CS = the first element of SL do

Eight queens puzzle Algorithm

Begin

Add CS to DE

Remove first element from SL

Remove first element from NSL

CS ~ first element of NSL

End

Add CS to SL

End

Eight queens puzzle Algorithm

Else begin

Place children of CS (except nodes already on DE SL or NSL) on NSL

CS = first element of NSL

Add CS to SL

End

End

Eight queens puzzle Algorithm

httpwwwcodeprojectcomArticles806248Eight-Queens-Puzzle-Tree-Algorithm

Complexity

The running time of your algorithm is at most N(Nminus1)(Nminus2)⋯(NminusK+1) ie N(NminusK) This is O(NK) ie exponential in K

httpwwwcheggcomhomework-helpquestions-and-answerspoor-man-s-n-queens-problemn-queens-arranged-n-x-n-chessboard-way-queen-checks-queen-queen-q1009394

  • Algorithm Strategies
  • Algorithms Strategy in Assignment
  • Roadmap of Every Strategy
  • Decrease and Conquer
  • Decrease and Conquer
  • Decrease and Conquer
  • Breadth-first search Algorithm
  • Algorithm of BFS
  • Order in which the nodes are expanded
  • Complexity
  • Greedy Approach
  • Uses of Greedy Approach
  • Greedy Approach (2)
  • Huffman Encoding
  • Huffman Encoding Algorithm
  • Working of the Huffman Encoding Algorithm
  • Complexity (2)
  • Knapsack Problem
  • Knapsack Problem
  • Knapsack Problem Variants
  • Greedy Knapsack - Fractional
  • Greedy Knapsack Algorithm
  • Example - Greedy Knapsack
  • Solution
  • Analysis
  • Backtracking
  • Backtracking (2)
  • Backtracking (3)
  • Eight queens puzzle
  • Eight queens puzzle Algorithm
  • Eight queens puzzle Algorithm (2)
  • Eight queens puzzle Algorithm (3)
  • Eight queens puzzle Algorithm
  • Complexity (3)
Page 26: Comparitive Analysis of Algorithm strategies

Backtracking

Backtracking is a technique for systematically trying all paths through a state space

Backtracking search begins at the start state and pursues a path until it reaches either a goal or a ldquodead endrdquo

If it finds a goal it quits and returns the solution path

If it reaches a dead end it ldquobacktracksrdquo to the most recent node on the path

having unexamined siblings and continues down one of these branches

enwikipediaorgwikiBacktracking

Backtracking

Backtracking is an important tool for solving

constraint satisfaction problems such as

crosswords

verbal arithmetic

Sudoku

and many other puzzles

8 Queen Puzzle Algorithm is an example of Backtracking

enwikipediaorgwikiBacktracking

Backtracking

Advantages

Quick test

Pair Matching

Following real life Concept

Disadvantages

Not widely Implemented

Cannot Express Left Recursion

More Time and Complexity

httpwwwclcamacuk~mr10backtrkpdf

Eight queens puzzle

The eight queens puzzle is the problem of placing eight chess queens on an 8times8 chessboard so that no two queens threaten each other

Thus a solution requires that no two queens share the same row column or diagonal

httpenwikipediaorgwikiEight_queens_puzzle

Eight queens puzzle Algorithm

Function backtrack

Begin

SL = [Start] NSL = [Start] DE = [] CS = Start initialize

While NSL ne [ ] do while there are states to be tried

Begin

If CS = goal (or meets goal description) then return SL on success return list of states in path

If CS has no children (excluding nodes already on DE SL and NSL) then

Begin

While SL is not empty and CS = the first element of SL do

Eight queens puzzle Algorithm

Begin

Add CS to DE

Remove first element from SL

Remove first element from NSL

CS ~ first element of NSL

End

Add CS to SL

End

Eight queens puzzle Algorithm

Else begin

Place children of CS (except nodes already on DE SL or NSL) on NSL

CS = first element of NSL

Add CS to SL

End

End

Eight queens puzzle Algorithm

httpwwwcodeprojectcomArticles806248Eight-Queens-Puzzle-Tree-Algorithm

Complexity

The running time of your algorithm is at most N(Nminus1)(Nminus2)⋯(NminusK+1) ie N(NminusK) This is O(NK) ie exponential in K

httpwwwcheggcomhomework-helpquestions-and-answerspoor-man-s-n-queens-problemn-queens-arranged-n-x-n-chessboard-way-queen-checks-queen-queen-q1009394

  • Algorithm Strategies
  • Algorithms Strategy in Assignment
  • Roadmap of Every Strategy
  • Decrease and Conquer
  • Decrease and Conquer
  • Decrease and Conquer
  • Breadth-first search Algorithm
  • Algorithm of BFS
  • Order in which the nodes are expanded
  • Complexity
  • Greedy Approach
  • Uses of Greedy Approach
  • Greedy Approach (2)
  • Huffman Encoding
  • Huffman Encoding Algorithm
  • Working of the Huffman Encoding Algorithm
  • Complexity (2)
  • Knapsack Problem
  • Knapsack Problem
  • Knapsack Problem Variants
  • Greedy Knapsack - Fractional
  • Greedy Knapsack Algorithm
  • Example - Greedy Knapsack
  • Solution
  • Analysis
  • Backtracking
  • Backtracking (2)
  • Backtracking (3)
  • Eight queens puzzle
  • Eight queens puzzle Algorithm
  • Eight queens puzzle Algorithm (2)
  • Eight queens puzzle Algorithm (3)
  • Eight queens puzzle Algorithm
  • Complexity (3)
Page 27: Comparitive Analysis of Algorithm strategies

Backtracking

Backtracking is an important tool for solving

constraint satisfaction problems such as

crosswords

verbal arithmetic

Sudoku

and many other puzzles

8 Queen Puzzle Algorithm is an example of Backtracking

enwikipediaorgwikiBacktracking

Backtracking

Advantages

Quick test

Pair Matching

Following real life Concept

Disadvantages

Not widely Implemented

Cannot Express Left Recursion

More Time and Complexity

httpwwwclcamacuk~mr10backtrkpdf

Eight queens puzzle

The eight queens puzzle is the problem of placing eight chess queens on an 8times8 chessboard so that no two queens threaten each other

Thus a solution requires that no two queens share the same row column or diagonal

httpenwikipediaorgwikiEight_queens_puzzle

Eight queens puzzle Algorithm

Function backtrack

Begin

SL = [Start] NSL = [Start] DE = [] CS = Start initialize

While NSL ne [ ] do while there are states to be tried

Begin

If CS = goal (or meets goal description) then return SL on success return list of states in path

If CS has no children (excluding nodes already on DE SL and NSL) then

Begin

While SL is not empty and CS = the first element of SL do

Eight queens puzzle Algorithm

Begin

Add CS to DE

Remove first element from SL

Remove first element from NSL

CS ~ first element of NSL

End

Add CS to SL

End

Eight queens puzzle Algorithm

Else begin

Place children of CS (except nodes already on DE SL or NSL) on NSL

CS = first element of NSL

Add CS to SL

End

End

Eight queens puzzle Algorithm

httpwwwcodeprojectcomArticles806248Eight-Queens-Puzzle-Tree-Algorithm

Complexity

The running time of your algorithm is at most N(Nminus1)(Nminus2)⋯(NminusK+1) ie N(NminusK) This is O(NK) ie exponential in K

httpwwwcheggcomhomework-helpquestions-and-answerspoor-man-s-n-queens-problemn-queens-arranged-n-x-n-chessboard-way-queen-checks-queen-queen-q1009394

  • Algorithm Strategies
  • Algorithms Strategy in Assignment
  • Roadmap of Every Strategy
  • Decrease and Conquer
  • Decrease and Conquer
  • Decrease and Conquer
  • Breadth-first search Algorithm
  • Algorithm of BFS
  • Order in which the nodes are expanded
  • Complexity
  • Greedy Approach
  • Uses of Greedy Approach
  • Greedy Approach (2)
  • Huffman Encoding
  • Huffman Encoding Algorithm
  • Working of the Huffman Encoding Algorithm
  • Complexity (2)
  • Knapsack Problem
  • Knapsack Problem
  • Knapsack Problem Variants
  • Greedy Knapsack - Fractional
  • Greedy Knapsack Algorithm
  • Example - Greedy Knapsack
  • Solution
  • Analysis
  • Backtracking
  • Backtracking (2)
  • Backtracking (3)
  • Eight queens puzzle
  • Eight queens puzzle Algorithm
  • Eight queens puzzle Algorithm (2)
  • Eight queens puzzle Algorithm (3)
  • Eight queens puzzle Algorithm
  • Complexity (3)
Page 28: Comparitive Analysis of Algorithm strategies

Backtracking

Advantages

Quick test

Pair Matching

Following real life Concept

Disadvantages

Not widely Implemented

Cannot Express Left Recursion

More Time and Complexity

httpwwwclcamacuk~mr10backtrkpdf

Eight queens puzzle

The eight queens puzzle is the problem of placing eight chess queens on an 8times8 chessboard so that no two queens threaten each other

Thus a solution requires that no two queens share the same row column or diagonal

httpenwikipediaorgwikiEight_queens_puzzle

Eight queens puzzle Algorithm

Function backtrack

Begin

SL = [Start] NSL = [Start] DE = [] CS = Start initialize

While NSL ne [ ] do while there are states to be tried

Begin

If CS = goal (or meets goal description) then return SL on success return list of states in path

If CS has no children (excluding nodes already on DE SL and NSL) then

Begin

While SL is not empty and CS = the first element of SL do

Eight queens puzzle Algorithm

Begin

Add CS to DE

Remove first element from SL

Remove first element from NSL

CS ~ first element of NSL

End

Add CS to SL

End

Eight queens puzzle Algorithm

Else begin

Place children of CS (except nodes already on DE SL or NSL) on NSL

CS = first element of NSL

Add CS to SL

End

End

Eight queens puzzle Algorithm

httpwwwcodeprojectcomArticles806248Eight-Queens-Puzzle-Tree-Algorithm

Complexity

The running time of your algorithm is at most N(Nminus1)(Nminus2)⋯(NminusK+1) ie N(NminusK) This is O(NK) ie exponential in K

httpwwwcheggcomhomework-helpquestions-and-answerspoor-man-s-n-queens-problemn-queens-arranged-n-x-n-chessboard-way-queen-checks-queen-queen-q1009394

  • Algorithm Strategies
  • Algorithms Strategy in Assignment
  • Roadmap of Every Strategy
  • Decrease and Conquer
  • Decrease and Conquer
  • Decrease and Conquer
  • Breadth-first search Algorithm
  • Algorithm of BFS
  • Order in which the nodes are expanded
  • Complexity
  • Greedy Approach
  • Uses of Greedy Approach
  • Greedy Approach (2)
  • Huffman Encoding
  • Huffman Encoding Algorithm
  • Working of the Huffman Encoding Algorithm
  • Complexity (2)
  • Knapsack Problem
  • Knapsack Problem
  • Knapsack Problem Variants
  • Greedy Knapsack - Fractional
  • Greedy Knapsack Algorithm
  • Example - Greedy Knapsack
  • Solution
  • Analysis
  • Backtracking
  • Backtracking (2)
  • Backtracking (3)
  • Eight queens puzzle
  • Eight queens puzzle Algorithm
  • Eight queens puzzle Algorithm (2)
  • Eight queens puzzle Algorithm (3)
  • Eight queens puzzle Algorithm
  • Complexity (3)
Page 29: Comparitive Analysis of Algorithm strategies

Eight queens puzzle

The eight queens puzzle is the problem of placing eight chess queens on an 8times8 chessboard so that no two queens threaten each other

Thus a solution requires that no two queens share the same row column or diagonal

httpenwikipediaorgwikiEight_queens_puzzle

Eight queens puzzle Algorithm

Function backtrack

Begin

SL = [Start] NSL = [Start] DE = [] CS = Start initialize

While NSL ne [ ] do while there are states to be tried

Begin

If CS = goal (or meets goal description) then return SL on success return list of states in path

If CS has no children (excluding nodes already on DE SL and NSL) then

Begin

While SL is not empty and CS = the first element of SL do

Eight queens puzzle Algorithm

Begin

Add CS to DE

Remove first element from SL

Remove first element from NSL

CS ~ first element of NSL

End

Add CS to SL

End

Eight queens puzzle Algorithm

Else begin

Place children of CS (except nodes already on DE SL or NSL) on NSL

CS = first element of NSL

Add CS to SL

End

End

Eight queens puzzle Algorithm

httpwwwcodeprojectcomArticles806248Eight-Queens-Puzzle-Tree-Algorithm

Complexity

The running time of your algorithm is at most N(Nminus1)(Nminus2)⋯(NminusK+1) ie N(NminusK) This is O(NK) ie exponential in K

httpwwwcheggcomhomework-helpquestions-and-answerspoor-man-s-n-queens-problemn-queens-arranged-n-x-n-chessboard-way-queen-checks-queen-queen-q1009394

  • Algorithm Strategies
  • Algorithms Strategy in Assignment
  • Roadmap of Every Strategy
  • Decrease and Conquer
  • Decrease and Conquer
  • Decrease and Conquer
  • Breadth-first search Algorithm
  • Algorithm of BFS
  • Order in which the nodes are expanded
  • Complexity
  • Greedy Approach
  • Uses of Greedy Approach
  • Greedy Approach (2)
  • Huffman Encoding
  • Huffman Encoding Algorithm
  • Working of the Huffman Encoding Algorithm
  • Complexity (2)
  • Knapsack Problem
  • Knapsack Problem
  • Knapsack Problem Variants
  • Greedy Knapsack - Fractional
  • Greedy Knapsack Algorithm
  • Example - Greedy Knapsack
  • Solution
  • Analysis
  • Backtracking
  • Backtracking (2)
  • Backtracking (3)
  • Eight queens puzzle
  • Eight queens puzzle Algorithm
  • Eight queens puzzle Algorithm (2)
  • Eight queens puzzle Algorithm (3)
  • Eight queens puzzle Algorithm
  • Complexity (3)
Page 30: Comparitive Analysis of Algorithm strategies

Eight queens puzzle Algorithm

Function backtrack

Begin

SL = [Start] NSL = [Start] DE = [] CS = Start initialize

While NSL ne [ ] do while there are states to be tried

Begin

If CS = goal (or meets goal description) then return SL on success return list of states in path

If CS has no children (excluding nodes already on DE SL and NSL) then

Begin

While SL is not empty and CS = the first element of SL do

Eight queens puzzle Algorithm

Begin

Add CS to DE

Remove first element from SL

Remove first element from NSL

CS ~ first element of NSL

End

Add CS to SL

End

Eight queens puzzle Algorithm

Else begin

Place children of CS (except nodes already on DE SL or NSL) on NSL

CS = first element of NSL

Add CS to SL

End

End

Eight queens puzzle Algorithm

httpwwwcodeprojectcomArticles806248Eight-Queens-Puzzle-Tree-Algorithm

Complexity

The running time of your algorithm is at most N(Nminus1)(Nminus2)⋯(NminusK+1) ie N(NminusK) This is O(NK) ie exponential in K

httpwwwcheggcomhomework-helpquestions-and-answerspoor-man-s-n-queens-problemn-queens-arranged-n-x-n-chessboard-way-queen-checks-queen-queen-q1009394

  • Algorithm Strategies
  • Algorithms Strategy in Assignment
  • Roadmap of Every Strategy
  • Decrease and Conquer
  • Decrease and Conquer
  • Decrease and Conquer
  • Breadth-first search Algorithm
  • Algorithm of BFS
  • Order in which the nodes are expanded
  • Complexity
  • Greedy Approach
  • Uses of Greedy Approach
  • Greedy Approach (2)
  • Huffman Encoding
  • Huffman Encoding Algorithm
  • Working of the Huffman Encoding Algorithm
  • Complexity (2)
  • Knapsack Problem
  • Knapsack Problem
  • Knapsack Problem Variants
  • Greedy Knapsack - Fractional
  • Greedy Knapsack Algorithm
  • Example - Greedy Knapsack
  • Solution
  • Analysis
  • Backtracking
  • Backtracking (2)
  • Backtracking (3)
  • Eight queens puzzle
  • Eight queens puzzle Algorithm
  • Eight queens puzzle Algorithm (2)
  • Eight queens puzzle Algorithm (3)
  • Eight queens puzzle Algorithm
  • Complexity (3)
Page 31: Comparitive Analysis of Algorithm strategies

Eight queens puzzle Algorithm

Begin

Add CS to DE

Remove first element from SL

Remove first element from NSL

CS ~ first element of NSL

End

Add CS to SL

End

Eight queens puzzle Algorithm

Else begin

Place children of CS (except nodes already on DE SL or NSL) on NSL

CS = first element of NSL

Add CS to SL

End

End

Eight queens puzzle Algorithm

httpwwwcodeprojectcomArticles806248Eight-Queens-Puzzle-Tree-Algorithm

Complexity

The running time of your algorithm is at most N(Nminus1)(Nminus2)⋯(NminusK+1) ie N(NminusK) This is O(NK) ie exponential in K

httpwwwcheggcomhomework-helpquestions-and-answerspoor-man-s-n-queens-problemn-queens-arranged-n-x-n-chessboard-way-queen-checks-queen-queen-q1009394

  • Algorithm Strategies
  • Algorithms Strategy in Assignment
  • Roadmap of Every Strategy
  • Decrease and Conquer
  • Decrease and Conquer
  • Decrease and Conquer
  • Breadth-first search Algorithm
  • Algorithm of BFS
  • Order in which the nodes are expanded
  • Complexity
  • Greedy Approach
  • Uses of Greedy Approach
  • Greedy Approach (2)
  • Huffman Encoding
  • Huffman Encoding Algorithm
  • Working of the Huffman Encoding Algorithm
  • Complexity (2)
  • Knapsack Problem
  • Knapsack Problem
  • Knapsack Problem Variants
  • Greedy Knapsack - Fractional
  • Greedy Knapsack Algorithm
  • Example - Greedy Knapsack
  • Solution
  • Analysis
  • Backtracking
  • Backtracking (2)
  • Backtracking (3)
  • Eight queens puzzle
  • Eight queens puzzle Algorithm
  • Eight queens puzzle Algorithm (2)
  • Eight queens puzzle Algorithm (3)
  • Eight queens puzzle Algorithm
  • Complexity (3)
Page 32: Comparitive Analysis of Algorithm strategies

Eight queens puzzle Algorithm

Else begin

Place children of CS (except nodes already on DE SL or NSL) on NSL

CS = first element of NSL

Add CS to SL

End

End

Eight queens puzzle Algorithm

httpwwwcodeprojectcomArticles806248Eight-Queens-Puzzle-Tree-Algorithm

Complexity

The running time of your algorithm is at most N(Nminus1)(Nminus2)⋯(NminusK+1) ie N(NminusK) This is O(NK) ie exponential in K

httpwwwcheggcomhomework-helpquestions-and-answerspoor-man-s-n-queens-problemn-queens-arranged-n-x-n-chessboard-way-queen-checks-queen-queen-q1009394

  • Algorithm Strategies
  • Algorithms Strategy in Assignment
  • Roadmap of Every Strategy
  • Decrease and Conquer
  • Decrease and Conquer
  • Decrease and Conquer
  • Breadth-first search Algorithm
  • Algorithm of BFS
  • Order in which the nodes are expanded
  • Complexity
  • Greedy Approach
  • Uses of Greedy Approach
  • Greedy Approach (2)
  • Huffman Encoding
  • Huffman Encoding Algorithm
  • Working of the Huffman Encoding Algorithm
  • Complexity (2)
  • Knapsack Problem
  • Knapsack Problem
  • Knapsack Problem Variants
  • Greedy Knapsack - Fractional
  • Greedy Knapsack Algorithm
  • Example - Greedy Knapsack
  • Solution
  • Analysis
  • Backtracking
  • Backtracking (2)
  • Backtracking (3)
  • Eight queens puzzle
  • Eight queens puzzle Algorithm
  • Eight queens puzzle Algorithm (2)
  • Eight queens puzzle Algorithm (3)
  • Eight queens puzzle Algorithm
  • Complexity (3)
Page 33: Comparitive Analysis of Algorithm strategies

Eight queens puzzle Algorithm

httpwwwcodeprojectcomArticles806248Eight-Queens-Puzzle-Tree-Algorithm

Complexity

The running time of your algorithm is at most N(Nminus1)(Nminus2)⋯(NminusK+1) ie N(NminusK) This is O(NK) ie exponential in K

httpwwwcheggcomhomework-helpquestions-and-answerspoor-man-s-n-queens-problemn-queens-arranged-n-x-n-chessboard-way-queen-checks-queen-queen-q1009394

  • Algorithm Strategies
  • Algorithms Strategy in Assignment
  • Roadmap of Every Strategy
  • Decrease and Conquer
  • Decrease and Conquer
  • Decrease and Conquer
  • Breadth-first search Algorithm
  • Algorithm of BFS
  • Order in which the nodes are expanded
  • Complexity
  • Greedy Approach
  • Uses of Greedy Approach
  • Greedy Approach (2)
  • Huffman Encoding
  • Huffman Encoding Algorithm
  • Working of the Huffman Encoding Algorithm
  • Complexity (2)
  • Knapsack Problem
  • Knapsack Problem
  • Knapsack Problem Variants
  • Greedy Knapsack - Fractional
  • Greedy Knapsack Algorithm
  • Example - Greedy Knapsack
  • Solution
  • Analysis
  • Backtracking
  • Backtracking (2)
  • Backtracking (3)
  • Eight queens puzzle
  • Eight queens puzzle Algorithm
  • Eight queens puzzle Algorithm (2)
  • Eight queens puzzle Algorithm (3)
  • Eight queens puzzle Algorithm
  • Complexity (3)
Page 34: Comparitive Analysis of Algorithm strategies

Complexity

The running time of your algorithm is at most N(Nminus1)(Nminus2)⋯(NminusK+1) ie N(NminusK) This is O(NK) ie exponential in K

httpwwwcheggcomhomework-helpquestions-and-answerspoor-man-s-n-queens-problemn-queens-arranged-n-x-n-chessboard-way-queen-checks-queen-queen-q1009394

  • Algorithm Strategies
  • Algorithms Strategy in Assignment
  • Roadmap of Every Strategy
  • Decrease and Conquer
  • Decrease and Conquer
  • Decrease and Conquer
  • Breadth-first search Algorithm
  • Algorithm of BFS
  • Order in which the nodes are expanded
  • Complexity
  • Greedy Approach
  • Uses of Greedy Approach
  • Greedy Approach (2)
  • Huffman Encoding
  • Huffman Encoding Algorithm
  • Working of the Huffman Encoding Algorithm
  • Complexity (2)
  • Knapsack Problem
  • Knapsack Problem
  • Knapsack Problem Variants
  • Greedy Knapsack - Fractional
  • Greedy Knapsack Algorithm
  • Example - Greedy Knapsack
  • Solution
  • Analysis
  • Backtracking
  • Backtracking (2)
  • Backtracking (3)
  • Eight queens puzzle
  • Eight queens puzzle Algorithm
  • Eight queens puzzle Algorithm (2)
  • Eight queens puzzle Algorithm (3)
  • Eight queens puzzle Algorithm
  • Complexity (3)