Beam Search Algorithm in Artificial Intelligence | All Imp Points | Heuristic Search Techniques

Gate Smashers
17 Dec 201912:02

Summary

TLDRThis video provides an in-depth explanation of the Beam Search algorithm, focusing on its importance in managing space complexity compared to the Best First Search algorithm. It highlights how Beam Search narrows down the best options by selecting only a limited number of nodes based on a 'beam width' or 'beta value,' enhancing efficiency. The tutorial also emphasizes key differences, like Beam Search’s potential incompleteness, and introduces related topics such as heuristic values, sorting techniques, and space complexity optimization. The video concludes with a teaser for the next topic, the Hill Climbing algorithm.

Takeaways

  • 🤖 Beam search algorithm is a variation of best first search, which focuses on improving space complexity.
  • 🧠 Both best first search and beam search are heuristic-based algorithms, helping choose the best path toward the goal state.
  • 🔍 Beam search differs from best first search by limiting the number of nodes it keeps in memory, based on a specified beam width (Beta).
  • 📉 The beam width (Beta) controls how many of the best nodes are kept, pruning the others to improve efficiency.
  • 🧮 Best first search keeps all nodes in memory and sorts them, leading to higher space complexity, while beam search only keeps the best nodes.
  • ⏳ Beam search improves space complexity, making it constant due to pruning unnecessary nodes.
  • 📊 Sorting methods like merge sort or quick sort are used in beam search, with fewer elements leading to faster sorting.
  • 🚧 Beam search sacrifices completeness, as it might prune the optimal path, leading to a potential dead end.
  • 🚀 Beam search is faster but might not always provide the optimal solution, as it limits exploration.
  • ⛰️ When the beam width is reduced to 1, the beam search algorithm becomes similar to the hill climbing method.

Q & A

  • What is the primary focus of the beam search algorithm?

    -The primary focus of the beam search algorithm is to optimize space complexity by limiting the number of nodes kept in memory, using a beam value to retain only the best nodes at each level.

  • How is beam search different from best first search?

    -Beam search is a variation of best first search. While best first search keeps all nodes in memory and sorts them, beam search limits the number of nodes using a beam value, pruning less optimal nodes to save space.

  • What is the heuristic value, and how is it used in these search algorithms?

    -The heuristic value is an estimated cost from the current node to the goal state. Both beam search and best first search use this value to prioritize nodes, choosing the paths with lower heuristic values for further exploration.

  • What is the role of the beam width (or beta) in beam search?

    -The beam width (or beta) determines how many nodes are retained at each level during the search. For example, if the beam width is 2, the algorithm keeps only the two best nodes based on their heuristic values, discarding the rest.

  • How does beam search help improve space complexity?

    -Beam search improves space complexity by pruning less optimal nodes based on the beam value, keeping only a limited number of nodes in memory. This reduces the overall space required compared to storing all nodes, as in best first search.

  • Why is beam search not considered a complete algorithm?

    -Beam search is not considered a complete algorithm because it prunes nodes based on the beam value. This pruning can lead to removing paths that may later prove optimal, potentially leading to dead ends and missing the best solution.

  • What happens if the beam value is set to 1?

    -If the beam value is set to 1, beam search becomes the hill climbing method. In this case, only the single best node is kept at each level, making the search highly greedy but potentially missing optimal solutions.

  • How does the time complexity of beam search compare to best first search?

    -Beam search reduces the time spent on sorting nodes because fewer nodes are retained in memory. While best first search must sort all explored nodes, beam search sorts only a limited set of nodes, making it more time-efficient in some cases.

  • What is the trade-off when using beam search compared to best first search?

    -The trade-off with beam search is that while it reduces space and time complexity by pruning nodes, it may miss the optimal path due to its incomplete nature. Best first search, on the other hand, explores all paths but at a higher cost in memory and time.

  • Can beam search provide an optimal solution every time?

    -No, beam search does not guarantee an optimal solution every time. Since it prunes nodes, it might discard the optimal path early in the search, leading to suboptimal results in certain cases.

Outlines

00:00

🔍 Introduction to Beam Search Algorithm

The video starts with an introduction to the Beam Search Algorithm, emphasizing its importance for competitive and academic exams. The host urges viewers to like, subscribe, and press the bell icon for notifications. The Beam Search Algorithm is introduced as a variation of the Best First Search Algorithm, which is based on heuristics. The speaker explains how the algorithm uses a starting state (A) and explores possible paths (B, C, D) by comparing their heuristic values. The example illustrates how the heuristic guides the search by choosing the path with the lowest cost (C), while keeping other paths (B, D) in memory using a priority queue. The discussion also covers sorting methods to manage the open list, ensuring that the lowest heuristic value is always explored first.

05:01

🧠 Memory and Pruning in Beam Search

This section delves deeper into the differences between Best First Search and Beam Search. In Best First Search, all explored nodes are kept in memory and sorted to prioritize the best path. However, Beam Search introduces a beam width, which limits the number of paths stored in memory. For example, if the beam width is 2, only the two best paths are retained, and others are pruned. This reduces space complexity by removing less optimal choices, making Beam Search more space-efficient than Best First Search. The concept of backtracking is also introduced, explaining how the algorithm may need to revisit earlier nodes if the current path leads to a dead end.

10:03

📉 Optimizing Space Complexity with Beam Search

This paragraph explains how Beam Search improves space complexity by pruning nodes based on the beam width. With fewer nodes in memory, both space and time complexity are reduced. The speaker discusses how Best First Search has both time and space complexity of O(b^d), but Beam Search reduces space complexity to a constant factor determined by the beam width. The importance of choosing the right beam value (beta) is highlighted, as a smaller beam width reduces memory usage and sorting time. The concept of branching factor and depth is explained in relation to exponential growth in search algorithms, with Beam Search providing an efficient way to manage these challenges.

⚠️ Limitations and Incompleteness of Beam Search

Here, the speaker discusses the limitations of Beam Search. Unlike Best First Search, which explores all nodes and ensures completeness, Beam Search may prune nodes that could lead to the optimal solution. This makes it a non-complete algorithm, meaning it might not always find the best or correct path, especially if a pruned path turns out to be the optimal one. The example illustrates how pruning can lead to dead ends or suboptimal solutions. The paragraph concludes by introducing the idea of Hill Climbing, which will be covered in the next video. The speaker wraps up the discussion by emphasizing the trade-off between space complexity and completeness in Beam Search.

Mindmap

Keywords

💡Beam Search Algorithm

Beam Search Algorithm is a heuristic-based search algorithm that limits the number of nodes stored in memory by keeping only the 'best' options at each step. In the video, it is highlighted as a variation of the Best First Search, where only a specific number of paths, based on a beam value (e.g., 2), are kept for exploration, reducing space complexity. The script explains this algorithm as an efficient way to handle larger search spaces.

💡Heuristic

A heuristic is a technique used in algorithms to make decisions that guide the search towards a goal by estimating which path might be more promising. In Beam Search, the heuristic value helps prioritize paths with lower estimated costs towards the goal. For instance, from node A, paths to B, C, and D are compared using heuristic values to decide which is best to explore further.

💡Best First Search

Best First Search is a search algorithm that explores paths based on the best heuristic value at each step. In the video, it is compared to Beam Search, with the key difference being that Best First Search maintains all possible paths in memory, leading to higher space complexity. The Beam Search algorithm is introduced as a more efficient variant.

💡Space Complexity

Space complexity refers to the amount of memory an algorithm requires as it runs. Beam Search is discussed as an improvement over Best First Search by reducing space complexity, which is controlled through the 'beam width' or 'beam value'. The fewer nodes kept in memory, the lower the space complexity. The video highlights how Beam Search can achieve constant space complexity.

💡Beam Width

Beam width (or beta value) is a parameter in Beam Search that limits how many of the best nodes are kept in memory during the search. If the beam width is 2, only the two best paths are retained, and others are discarded. The video demonstrates how this impacts memory usage and the overall efficiency of the algorithm.

💡Priority Queue

A priority queue is a data structure used in search algorithms to manage nodes based on their priority, typically determined by heuristic values. In the video, the nodes are inserted into a priority queue where they are sorted and the best node (lowest heuristic value) is chosen for further exploration. The Beam Search algorithm simplifies this process by limiting the number of nodes in the queue.

💡Sorting

Sorting refers to the process of ordering nodes based on their heuristic values in the priority queue. The video explains how sorting is used in Best First Search to determine which node should be explored next. In Beam Search, sorting still occurs, but fewer nodes are sorted due to the pruning effect of the beam width, reducing the computational burden.

💡Pruning

Pruning in Beam Search refers to the removal of less promising nodes from the search process based on the beam width. Nodes that don't rank among the top choices are discarded, thus reducing memory usage and improving efficiency. The video emphasizes pruning as a key factor in reducing space complexity, making Beam Search more efficient than Best First Search.

💡Branching Factor

The branching factor represents the number of possible child nodes or states that can be generated from a given node in a search algorithm. The video mentions that Beam Search reduces the effective branching factor by pruning less optimal nodes, which helps manage space complexity. The branching factor plays a crucial role in determining the search's time and space requirements.

💡Hill Climbing Algorithm

Hill Climbing is a search algorithm where only the best option at each step is pursued, similar to a Beam Search with a beam width of 1. The video briefly introduces this concept, explaining that if Beam Search limits the beam value to 1, it behaves like Hill Climbing, where only the most promising node is explored without backtracking.

Highlights

Introduction to Beam Search algorithm and its importance for competitive and university exams.

Beam Search is a variation of Best First Search algorithm that focuses on optimizing space complexity.

Beam Search and Best First Search both use heuristics to guide the search process.

The concept of heuristic values is explained using an example, where C is the best choice as it has the lowest cost.

Best First Search algorithm uses sorting to manage the priority queue and determine which nodes to explore next.

In Beam Search, nodes that are not explored are removed from the memory, improving space efficiency.

Beam width (or Beta) determines how many nodes are kept in memory; a smaller beam width results in fewer nodes.

For example, with a beam value of 2, only the best two choices are kept in memory while others are discarded.

Beam Search uses a greedy method to explore nodes based on the lowest heuristic value.

The advantage of Beam Search is that it reduces space complexity by limiting the number of nodes stored in memory.

Beam Search can fail to find the optimal solution because it prunes potentially correct paths.

Beam Search has constant space complexity, but its completeness is not guaranteed.

When the beam value is set to 1, Beam Search becomes a Hill Climbing algorithm.

Beam Search is not guaranteed to provide the most optimal solution, as it may prune the correct path during exploration.

In the next video, Hill Climbing algorithm, which is closely related to Beam Search, will be discussed.

Transcripts

play00:00

Hello friends welcome to Gate Smashers

play00:02

In this video we are going to discuss

play00:04

Beam search algorithm

play00:06

And in this video related to beam search algorithm

play00:08

We are going to discuss all the important points

play00:10

Which in your competitive exams

play00:12

Or for your college or university level exams also they will be very helpful

play00:17

So guys quickly like the video

play00:19

Subscribe the channel if you have still not done

play00:21

And please press the bell button

play00:22

So that you can get all the latest notifications

play00:25

So we will start beam search algorithm

play00:28

First of all

play00:29

Beam search algorithm takes care related to what

play00:33

Related to space complexity

play00:35

As we had discussed in the last video

play00:37

Best first algorithm

play00:39

And best first search algorithm

play00:42

Or if we talk about beam search algorithm

play00:44

Both are based on heuristic

play00:46

In best first search

play00:48

We had discussed same example over here

play00:50

In this example I have made some changes

play00:53

I have added an extra edge

play00:55

So that you can understand it with more clarity

play00:58

So if we apply best first search over here

play01:00

Which we have seen in the last video

play01:02

If you have not checked that video then definitely check it once

play01:04

Because this beam search is variation of best first search only

play01:09

It is modified a bit

play01:10

And we have given it name of a new algorithm

play01:13

That is beam search algorithm

play01:15

What is that change

play01:17

Which was not there in best first search

play01:19

We are actually going to discuss that change in this video

play01:22

So as over here we have starting state A

play01:25

And goal state is G

play01:26

So if A is my starting state

play01:29

Then how many choices I have from A

play01:31

Either I can go from A to B

play01:34

Or I can go to C

play01:36

Or I can go to D

play01:39

I have 3 choices over here

play01:40

From A you can go to B,C or D

play01:44

And what do we check over here?

play01:46

Heuristic value

play01:47

What is the meaning of heuristic value?

play01:49

That B is taking me to the goal state, with 32 cost

play01:53

B is taking me with 32 cost

play01:55

We have already calculated this heuristic value

play01:58

I had told you in the last video that how we had calculated

play02:01

That is called a straight distance

play02:03

Over here we have done straight line distance calculation

play02:05

C is taking me to the goal state with the cost of 25

play02:10

D is taking to the goal state with the cost of 35

play02:15

And what do we apply in breadth first search

play02:18

The one which will be having minimum heuristic

play02:21

Whose heuristic value will be the best

play02:24

What is the meaning of best, the minimum the value the best the answer

play02:28

So which is the minimum value over here? C

play02:30

So C is taking me in the minimum cost to the goal state

play02:34

So I will explore C ahead

play02:36

The concept is very simple

play02:38

Of best search algorithm

play02:39

Because the name itself indicates best search

play02:41

So if over here we compare ahead of C

play02:45

Then what we will do about B&D

play02:47

We keep B&D in the memory

play02:50

And when we maintain priority queue how we do that

play02:54

First of all A came

play02:55

We removed A

play02:57

After that we explored B, C&D

play03:00

Now which is the minimum from them

play03:02

C so which is after C

play03:04

After C we have B

play03:05

And which is there after B

play03:07

D

play03:07

What you have applied over here

play03:09

Over here we have used sorting

play03:12

We have used sorting method over here

play03:14

So that I can come to know which is the minimum

play03:17

So that I will remove that one first from the priority queue

play03:21

This is called a open list

play03:23

So to remove from open list I have applied sorting algorithm over here

play03:26

Now I will remove C from here

play03:28

And I will further explore C

play03:31

What about B and D

play03:32

B&D are still there in the memory

play03:35

That means they are already there in the queue

play03:37

Now what will I do I will go on C, so when I went on C

play03:40

Then what option do I have from C

play03:42

From C I have option

play03:43

Either I can go to D

play03:45

Or I can go to F

play03:48

I have F option from C

play03:50

I have option of E

play03:51

Or have option of A from C

play03:54

So there is no need to again go to A from C

play03:56

Because you have already came from A to C

play03:58

And you have already explored A

play04:00

So if you want to make C to A then you can make it

play04:03

But there is no benefit of this

play04:05

Because A is already explored

play04:07

So from open list we will put it into closed list

play04:09

The meaning of closed list is those that are already explored

play04:12

You put across on them and you are not going to explore them again

play04:15

In case I take these 3 options and go ahead

play04:18

So how will these 3 options take me

play04:20

D is taking me in 35 cost

play04:22

F is taking me in 17 cost

play04:26

And E is taking me in 19 cost

play04:30

So again what will I do I added these 3 in priority queue

play04:34

First of all we will put F

play04:36

Then I will put E

play04:37

Then I will put D

play04:39

D is already there

play04:40

And after that I will compare these 4

play04:43

I will compare value of all 4

play04:44

We will again sort them

play04:46

And after sorting I will come to know

play04:48

That the value of F is the minimum

play04:51

So F will come ahead in these 4

play04:53

So that I can further explore F

play04:58

This is the simple concept

play04:59

Of best first search algorithm

play05:01

But over here what you need to understand actually is

play05:03

Over here you have to understand the first important point

play05:05

The node that you are not exploring

play05:08

You are keeping them also in the memory

play05:11

You are keeping them also in the memory and when you are doing sorting

play05:15

At that time they are participating

play05:16

Obviously if they are in the memory then you have to make them participate overall

play05:20

Because the best first search

play05:22

It makes a global list and goes on

play05:24

Global list means

play05:26

All those you have explored till now

play05:28

All the names that you have written till now

play05:30

That all are there in the open list or in the open priority queue

play05:35

But what does beam search tells

play05:37

Beam search is telling why you are keeping all these elements in the priority queue

play05:42

Why you are keeping all these in priority queue

play05:45

Just based on some beam value

play05:48

You will be given beam value according to the question

play05:50

Let's say if I talk over here beam width

play05:53

If I give you beam with or Beta value as 2

play05:56

Sometimes it is called as beta sometimes it is written as n

play06:00

Based on the beam value let's see if I give you beam value is 2

play06:03

Then what is the meaning of 2

play06:05

That we have to keep best 2

play06:07

Remove others

play06:08

Like you started the journey from A

play06:11

You had 3 options B, C and D

play06:14

You came to know that from these 3 C is better

play06:17

After that B is better

play06:19

After that D is better

play06:20

If you are beam search value is 2

play06:23

That means you need not keep all the choices

play06:25

You keep only best 2 from all the choices

play06:29

That is the simple point

play06:30

So 2 best means you can keep C and B

play06:32

That means you can explore C

play06:34

Even if you want B

play06:36

If in case C will take me in future to a dead end

play06:39

Obviously we are using kind of greedy method

play06:42

So the one which I find the best that yes I can follow this path

play06:46

This is the minimum

play06:47

You went on that

play06:48

Then you went ahead and asked if you are travelling in any city

play06:51

You don't know anything about that city

play06:53

You don't have any map

play06:54

Then you are asking someone going ahead

play06:56

So in case if you get some wrong heuristic

play06:59

If someone is telling you a wrong way

play07:01

Then obviously somewhere you can go on a dead end also

play07:04

We are not going in this example

play07:05

But somewhere you can also go on a dead end state

play07:07

So in that case you will have to backtrack

play07:09

You will have to go to a further state

play07:11

Then it is possible that you have to also Explore B

play07:13

But what is beam search telling

play07:15

That you keep just best 2

play07:17

Remove others

play07:19

That means remove D from here

play07:21

You don't have to explore D

play07:24

You don't have to put D into the list

play07:26

The concept is very simple

play07:27

Now let's say that you explored C

play07:30

You have kept B into the memory

play07:32

You explored C

play07:33

You have option D, F and E

play07:35

Let's suppose

play07:36

We have already removed D from here

play07:38

But just for example if we consider it

play07:40

So again what value is given over here 2

play07:44

So 2 means, the 2 best heuristics

play07:48

Keep them and remove the others

play07:51

What will happen due to this

play07:52

Your space complexity will be better

play07:56

Because BFS the best first search algorithm was there

play08:00

In best first search time complexity was O(b^d)

play08:04

And space complexity was O(b^d)

play08:07

What is b it is a branching factor

play08:09

Branching factor means if it is my starting node

play08:12

We explored them from starting node

play08:14

After exploring them again I explored them

play08:17

All of them and this is your branching value

play08:21

And the more depth you will go

play08:22

It will become an exponential complexity

play08:25

Time also in space also

play08:27

So what benefit you are getting from beam

play08:29

That from starting state if you explored further states

play08:35

So now based on the beam value

play08:37

Beam value is 2

play08:39

So you take 2 best

play08:40

Remove others

play08:42

That means you don't have to keep them into the memory

play08:44

Now let's say you explored them

play08:47

Then further there are multiple options

play08:49

Again you have to take 2 best

play08:51

Let's say these 2 are best

play08:52

Remove others

play08:54

So obviously what will be better than that

play08:56

Due to this your space complexity will be better

play09:01

So you can guess yourself how much will be the space complexity

play09:04

Space complexity will be constant

play09:07

Because all the values

play09:09

The value of beta will be less

play09:12

That much more nodes you are pruning that means you are removing

play09:17

So by default if you will remove the beam search

play09:21

If we talk about best first search

play09:23

Then you can see in the case of best first search

play09:26

By default what is the beam with

play09:28

Infinite

play09:29

Because you are not pruning any value

play09:32

You are keeping all of them in the memory

play09:34

So the more you will reduce the beam value

play09:38

That much more nodes you will remove

play09:41

You will go on pruning

play09:42

So obviously your space complexity will be better

play09:44

That is a constant space complexity

play09:47

Second there will also be difference in time

play09:48

How? The sorting that you are doing

play09:51

Obviously for sorting you have multiple method

play09:54

Merge sort quick sort

play09:55

There are many complexity methods

play09:57

So generally the complexity is L(log n)

play10:00

Sometimes in worst case it also goes up to n^2

play10:03

But if you consider on an average

play10:05

That whichever sorting method you are using over here

play10:08

So when number of nodes will be reduced

play10:11

Then it will be easy to sort

play10:13

So that is why beam search is actually used

play10:16

So for beam search you can say that we have modified best first search a bit

play10:22

In terms of how many values we are pruning

play10:25

So how is this value found

play10:27

Generally this value will be given to us

play10:30

But somewhere on the backend based on some heuristic values

play10:33

Based on lot of experiments

play10:36

I will have to fix this value

play10:38

Because in this case there can also be problem somewhere

play10:40

What can be the problem

play10:42

Best first search takes the global list

play10:45

It will consider all

play10:46

It will consider all in the list

play10:48

So it is a complete algorithm

play10:50

But, you cannot say beam search as complete

play10:53

What is the reason

play10:55

Same example I will be giving

play10:56

You picked up 2 best

play10:58

You removed others

play10:59

Ahead of this also again you picked 2 best

play11:01

You removed others

play11:02

It is possible that it can take you ahead to a dead end

play11:06

And let's say this is taking you to the right path

play11:11

But you have already pruned the right path

play11:14

So this completeness is not over here

play11:17

Yes it can give you a good solution or quality somewhere

play11:21

But there is no guarantee of always providing an optimal solution

play11:25

Show in beam search always remember this as the main point

play11:28

Take care of the space complexity is constant

play11:30

And somewhere you will be given b value

play11:33

Over here there is some difference in time because we have to do sorting in less elements

play11:39

And in this I want to tell the last point over here

play11:42

If the value of beta

play11:43

If beam value is given as one

play11:46

1

play11:47

If I give you that the value of b is just one

play11:49

Then we call it as hill climbing method

play11:52

So we are going to discuss hill climbing algorithm in the next video

play11:55

That is all about the beam search algo

play11:59

Thank you

Rate This

5.0 / 5 (0 votes)

Related Tags
Beam SearchBest First SearchAlgorithm TutorialHeuristic SearchSpace ComplexityCompetitive ExamsProgramming GuideAI AlgorithmsComputer ScienceSorting Techniques