איתור הנתיבים הקצרים ביותר באמצעות חיפוש ראשון ברוחב

האם אתה יודע את כמות התעבורה האווירית העולמית בשנת 2017? האם אתה יודע מה עליית התנועה האווירית בשנים האחרונות? ובכן, בואו נסתכל על נתונים סטטיסטיים.

על פי ארגון התעופה האזרחית הבינלאומי (ICAO), שיא של 4.1 מיליארד נוסעים הועבר על ידי תעשיית התעופה בשירותים סדירים בשנת 2017. ומספר הטיסות עלה ל -37 מיליון ברחבי העולם בשנת 2017.

זה הרבה נוסעים והרבה טיסות שתופסות את החלל האווירי על בסיס יומי ברחבי העולם. מכיוון שיש מאות ואלפי טיסות אלה ברחבי העולם, יתכנו מסלולים שונים עם מספר עצירות בין מקום למקום אחר.

לכל טיסה מקור ויעד משלה ומחיר מושב סטנדרטי לכלכלה המשויך אליה. בואו נעזוב את הכרטיסים המהודרים למחלקת עסקים וחדר הרגליים הנוסף ומה לא!

בתרחיש כזה, מבלבל מדי לבחור איזו טיסה תהיה הטובה ביותר אם אנו רוצים לעבור ממקום למקום אחר.

בוא נראה את מספר אפשרויות הטיסה ש- StudentUniverse (מספק הנחות לסטודנטים?) נותן לי מלוס אנג'לס לניו דלהי.

119 טיסות בסך הכל מוצעות. ואז מופיע קופץ באתר שאומר שישנם אתרים אחרים שעשויים להציע טיסות דומות במחירים זולים עוד יותר. ?

כל כך הרבה אתרים וטיסות לא סופרות למקור ויעד בודדים בלבד.

כמפתח, אם ברצוני לפתור בעיה זו, הייתי בונה מערכת שתטפל ביעילות בשאילתות הבאות:

  • המספר הכולל של היעדים הנגישים אליהם (עם מספר עצירות מרבי) מהמיקום הנוכחי שלי, וכן ציין את היעדים האלה.

    צריך לשמור על האפשרויות שלהם פתוחות כאשר הם רוצים לנסוע?.

  • עובדה ידועה (IMO?) כי קו עם מספר עצירות נוטה להוות אלטרנטיבה זולה יותר לטיסות ישירות.

    לכן, בהינתן מקור ויעד, אולי נרצה למצוא מסלולים עם לפחות 2 או 3 תחנות.

  • והכי חשוב: מהו המסלול הזול ביותר ממקור נתון ליעד נתון?
  • ו .... בסוף נגיע לזה?

כפי שאתה יכול לנחש, ייתכן שיהיו אלפי טיסות כפלט של שתי השאילתות הראשונות. אך אנו בהחלט יכולים להפחית זאת על ידי מתן כמה קריטריונים אחרים להפחתת גודל התפוקה. להיקף מאמר זה, הבה נתמקד בשאילתות מקוריות אלה עצמן.

דוגמנות רשת הטיסות כגרף

מהכותרת של מאמר זה די ברור שגרפים יהיו מעורבים באיזשהו מקום, לא?

דוגמנות בעיה זו כבעיית מעבר גרפי מפשטת אותה מאוד והופכת את הבעיה להרבה יותר ניתנת לביצוע. לכן, כצעד ראשון, בואו נגדיר את הגרף שלנו.

אנו מדגמנים את התנועה האווירית כ:

  • מְכוּוָן
  • אולי מחזורי
  • מְשׁוּקלָל
  • יַעַר. G (V, E)

מכוונים מכיוון שלכל טיסה יהיה מקור ייעודי ויעד. אלה נושאים משמעות רבה.

מחזורית כי אפשר מאוד לעקוב אחר חבורת טיסות שמתחילות ממיקום נתון ומסתיימות חזרה באותו מיקום.

משוקלל מכיוון שלכל טיסה יש עלות המשויכת שהיא כרטיס הטיסה במחלקת תיירים למאמר זה.

ולבסוף, יער כי ייתכן שיש לנו מספר רכיבים מחוברים. אין צורך שבכל הערים בעולם תהיה איזושהי רשת טיסות. לכן, ניתן לנתק את הגרף, ומכאן יער.

הקודקודים, V , יהיו המיקומים בכל רחבי העולם בכל מקום שבו ישנם שדות תעופה עובדים.

הקצוות, E , יהיו מייצגים את כל הטיסות המהוות את התנועה האווירית. קצה מ u -->; פשוט פירושו שיש לך טיסה מכוונת מהמקום / לא de u tov.

כעת, כשיש לנו מושג כיצד לדגם את רשת הטיסות כגרף, נעבור ונפתור את השאילתה הנפוצה הראשונה שעשויה להיות למשתמש.

המספר הכולל של היעדים הנגישים

מי לא אוהב לטייל?

כמי שאוהב לחקור מקומות שונים, היית רוצה לדעת מה ניתן להגיע לכל היעדים משדה התעופה המקומי שלך. שוב, יהיו כאן קריטריונים נוספים להפחתת התוצאות של שאילתה זו. אך כדי לשמור על פשטות, ננסה למצוא את כל המיקומים אליהם ניתן להגיע משדה התעופה המקומי שלנו.

כעת, כשיש לנו גרף מוגדר היטב, אנו יכולים ליישם אלגוריתמי מעבר כדי לעבד אותו.

החל מנקודה מסוימת, נוכל להשתמש בחיפוש ראשון ברוחב (BFS) או בעומק חיפוש ראשון (DFS) כדי לחקור את הגרף או את המיקומים אליהם ניתן להגיע ממיקום ההתחלה תוך מספר עצירות מרבי. מכיוון שמאמר זה עוסק בסך הכל באלגוריתם החיפוש הראשון, נבחן כיצד נוכל להשתמש ב- BFS המפורסם בכדי לבצע משימה זו.

נתחל את תור ה- BFS עם המיקום הנתון כנקודת ההתחלה. לאחר מכן אנו מבצעים את החצייה הראשונה, וממשיכים עד שהתור ריק או עד למיצוי המספר המרבי של עצירות.

הערה: אם אינך מכיר את החיפוש הראשון או את החיפוש הראשון, הייתי ממליץ לעבור על מאמר זה לפני שתמשיך.

בואו נסתכל על הקוד לאתחל את מבנה נתוני הגרף שלנו. עלינו גם לבדוק כיצד אלגוריתם BFS יביא לנו את כל היעדים שניתן להגיע אליהם ממקור נתון.

כעת, כשיש לנו מושג טוב לגבי האופן שבו אתחול הגרף, בואו נסתכל על הקוד לאלגוריתם BFS.

הופעה bfsבעיר לוס אנג'לס תיתן לנו את היעדים הבאים אליהם ניתן להגיע:

{'Chicago', 'France', 'Ireland', 'Italy', 'Japan', 'New Delhi', 'Norway'}

זה היה פשוט, לא?

נבדוק כיצד אנו יכולים להגביל את ה- BFS למספר עצירות מרבי בהמשך המאמר.

במקרה שיש לנו רשת טיסות ענקית, שתהיה לנו בתרחיש ייצור, לא נרצה באופן אידיאלי לחקור את כל היעדים הנגישים מנקודת התחלה נתונה.

זהו מקרה שימוש אם רשת הטיסות קטנה מאוד או נוגעת רק לאזורים ספורים בארצות הברית.

אבל עבור רשת גדולה, מקרה שימוש מציאותי יותר יהיה למצוא את כל נתיבי הטיסה עם מספר עצירות. הבה נבחן את הבעיה בפירוט רב יותר ונראה כיצד נוכל לפתור אותה.

מסלולים עם עצירות מרובות

עובדה ידועה היא שלעתים קרובות יותר, עבור מקור ויעד נתון, נסיעה מרובת עצירות זולה יותר מטיסה ישירה ללא הפסקה.

A lot of times we prefer the direct flight to avoid the layovers. Also because the multi-stop flights do tend to take a lot of time — which we don’t have.

However, if you don’t have any deadlines approaching and you want to save some bucks (and are comfortable with the multi-stop route that a lot of airlines suggest), then you might actually benefit a lot from something like this.

Also, you might get to pass through some of the most beautiful locations in the world with some of the most advanced airports which you can enjoy. So, that’s enough motivation as it is.

In terms of the graph model that we have been talking about, given a source and a destination, we need to come up with routes with 2 or more stops for a given source and destination.

As an end user, we might not want to see flights in this order for this query:

[A, C, D, B], 2 stops, $X[A, E, D, C, F, W, G, T, B], 8 stops, $M[A, R, E, G, B], 3 stops, $K[A, Z, X, C, V, B, N, S, D, F, G, H, B, 11 stops, $P

I know. Nobody in their right minds would want to go for a flight route with 11 stops. But the point I’m trying to make is that an end user would want symmetry. Meaning that they would want to see all the flights with 2 stops first, then all the flights with 3 stops and so on till maybe a max of, say, 5 stops.

But, all the flight routes with the same number of stops in between should be displayed together. That is a requirement we need to satisfy.

Let’s look at how we can solve this. So, given the graph of flight networks, a source S and a destination D, we have to perform a level order traversal and report flight routes from S -->; D with at least 2 and at most 5 stops in between. This means we have to do a level order traversal until a depth of 7 from the start node S .

Have a look at the code for solving this problem:

This might not be the best way to go about solving this problem at scale — the largest memory constraint would be due to the nodes currently present in the queue.

Since every node or location can have thousands of flights to other destinations in the world, the queue could be humongous if we store actual flight data like this. This is just to demonstrate one of the use cases of the breadth first search algorithm.

Now, let us just focus on the traversal and look at the way it is done. The traversal algorithm is simple as it is. However, the entire space complexity of the level order traversal comes from the elements in the queue and the size of each element.

There are multiple ways to implement the algorithm. Also, each of them varies in terms of maximum memory consumed at any given time by the elements in the queue.

We want to see the maximum memory consumed by the queue at any point in time during the level order traversal. Before that, let’s construct a random flight network with random prices.

Now let us look at the implementation of level order traversal.

This above is the easiest and most straightforward implementation of the level order traversal algorithm.

With every node we add to the queue, we also store the level information and we push a tuple of (node, level) into the queue. So every time we pop an element from the queue, we have the level information attached with the node itself.

The level information for our use case would mean the number of stops from the source to this location in the flight route.

It turns out that we can do better as far as memory consumption of the program is concerned. Let us look at a slightly better approach to doing level order traversal.

The idea here is that we don’t store any additional information with the nodes being pushed into the queue. We use a None object to mark the end of a given level. We don’t know the size of any level before hand except for the first level, which just has our source node.

So, we start the queue with [source, None] and we keep popping elements. Every time we encounter a None element, we know that a level has ended and a new one has started. We push another None to mark the end of this new level.

Consider a very simple graph here, and then we will dry run this through the graph.

**************************************************** LEVEL 0 beginslevel = 0, queue = [A, None]level = 0, pop, A, push, B, C, queue = [None, B, C]pop None ******************************************* LEVEL 1 beginspush Nonelevel = 1, queue = [B, C, None]level = 1, pop, B, push, C, D, F, queue = [C, None, C, D, F]level = 1, pop, C, push, D, D (lol!), queue = [None, C, D, F, D, D]pop None ******************************************* LEVEL 2 beginspush Nonelevel = 2, queue = [C, D, F, D, D, None] .... and so on

I hope this sums up the algorithm pretty well. This certainly is a neat trick to do level order traversal, keep track of the levels, and not encounter too much of a memory concern. This certainly reduces the memory footprint of the code.

Don’t get complacent now thinking this is a great improvement.

It is, but you should be asking two questions:

  1. How big of an improvement is this?
  2. Can we do better?

I will answer both of these questions now starting with the second question. The answer to that is Yes!

We can do one better here and completely do away with the need for the None in the queue. The motivation for this approach comes from the previous approach itself.

If you look closely at the dry run above, you can see that every time we pop a None, one level is finished and the other one is ready for processing. The important thing is that an entire next level exists in the queue at the time of popping of a None . We can make use of this idea of considering the queue size into the traversal logic.

Here is the pseudo code for this improved algorithm:

queue = Queue()queue.push(S)level = 0while queue is not empty { size = queue.size() // size represents the number of elements in the current level for i in 1..size { element = queue.pop() // Process element here // Perform a series of queue.push() operations here
 level += 1

And here is the code for the same.

The pseudo code is self explanatory. We essentially do away with the need for an extra None element per level and instead make use of the queue’s size to change levels. This would also lead to improvement over the last method, but how much?

Have a look at the following Jupyter Notebook to see the memory difference between the three methods.

  • We track the maximum size of the queue at any time by considering the sum of sizes of individual queue elements.
  • According to Python’s documentation, sys.getsizeof returns the object’s pointer or reference’s size in bytes. So, we saved almost 4.4Kb space (20224 — 15800 bytes) by switching to the None method from the original level order traversal method. This is just the memory savings for this random example, and we went only until the 5th level in the traversal.
  • The final method only gives an improvement of 16 bytes over the None method. This is because we got rid of just 4 None objects which were being used to mark the 4 levels (apart from the first one) that we processed. Each pointer’s size (pointer to an object) is 4 bytes in Python on a 32 bit system.

Now that we have all these interesting multi-path routes from our source to our destination and highly efficient level order traversal algorithms to solve it, we can look at a more lucrative problem to solve using our very own BFS.

What’s the cheapest flight route from my source to a given destination? This is something everybody would be instantly interested in. I mean who doesn’t want to save some bucks?

Shortest Path from a given source to destination

There’s not much description to give for the problem statement. We just need to find the shortest path and make the end user happy.

Algorithmically, given a weighted directed graph, we need to find the shortest path from source to destination. Shortest or cheapest would be one and the same thing from the point of the view of the algorithm.

We will not go into describing a possible BFS solution to this problem because such a solution would be intractable. Let us look at the graph below to understand why that is the case.

We say that BFS is the algorithm to use if we want to find the shortest path in an undirected, unweighted graph. The claim for BFS is that the first time a node is discovered during the traversal, that distance from the source would give us the shortest path.

The same cannot be said for a weighted graph. Consider the graph above. If say we were to find the shortest path from the node A to B in the undirected version of the graph, then the shortest path would be the direct link between A and B. So, the shortest path would be of length 1 and BFS would correctly find this for us.

However, we are dealing with a weighted graph here. So, the first discovery of a node during traversal does not guarantee the shortest path for that node. For example, in the diagram above, the node B would be discovered initially because it is the neighbor of A and the cost associated with this path (an edge in this case) would be 25 . But, this is not the shortest path. The shortest path is A --> M --> E --> B of length 10.

Breadth first search has no way of knowing if a particular discovery of a node would give us the shortest path to that node. And so, the only possible way for BFS (or DFS) to find the shortest path in a weighted graph is to search the entire graph and keep recording the minimum distance from source to the destination vertex.

This solution is not feasible for a huge network like our flight network that would have potentially thousands of nodes.

We won’t go into the details of how we can solve this. That is out of scope for this article.

What if I told you that BFS is just the right algorithm to find the shortest path in a weighted graph with a slight constraint ?

Constrained Shortest Paths

Since the graph we would have for the flight network is humongous, we know that exploring it completely is not really a possibility.

Consider the problem of shortest paths from the customer’s perspective. When you want to book a flight, these are the following options you ideally consider:

  • It shouldn’t be too long a flight.
  • It should be under your budget (Very Important).
  • It may have multiple stops but not more than K where K can vary from person to person.
  • Finally we have personal preferences which involve things like lounge access, food quality, layover locations, and average leg room.

The important point to consider here is the third one above: it may have multiple stops, but not more than K where K can vary from person to person.

A customer wants the cheapest flight route, but they also don’t want say 20 stops in between their source and destination. A customer might be okay with a maximum of 3 stops, or in extreme cases maybe even 4 — but not more than that.

We would want an application that would find out the cheapest flight route with at most K stops for a given source and destination.

This question from LeetCode has been the primary motivation for me to write this article. I strongly recommend going through the question once and not only relying on the snapshot above.

“Why would BFS work here?” one might ask. “This is also a weighted graph and the same reason for the failure of BFS that we discussed in the previous section should apply here.” NO!

The number of levels that the search would go to is limited by the value K in the question or in the description provided at the start of section. So, essentially, we would be trying to find the shortest path, but we won’t have to explore the entire graph as such. We will just go up to the level K.

From a real life scenario, the value of K would be under 5 for any sane traveler ?.

Let us look at the pseudo-code for the algorithm:

def bfs(source, destination, K): min_cost = dictionary representing min cost under K stops for each node reachable from source. 
 set min_cost of source to be 0
 Q = queue() Q.push(source) stops = 0 while Q is not empty {
 size = Q.size for i in range 1..size { element = Q.pop() 
 if element == destination then continue
 for neighbor in adjacency list of element { if stops == K and neighbor != destination then continue 
 if min_cost of neighbor improves, update and add back to the queue. } } stops ++ }

This again is level order traversal and the approach being used here is the one that makes use of the queue’s size at every level. Let us look at a commented version of the code to solve this problem.

Essentially, we keep track of the minimum distance of every node from the given source. The minimum distance for the source would be 0 and +inf for all others initially.

Whenever we encounter a node, we check if the current minimum path length can be improved or not. If it can be improved, that means that we have found an alternate path from source to this vertex with cheaper cost — a cheaper flight route until this vertex. We queue this vertex again so that locations and nodes reachable from this vertex on are updated (may or may not be) as well.

The key thing is this:

# No need to update the minimum cost if we have already exhausted our K stops. if stops == K and neighbor != dst: continue

So we just popped the a node represented by element in the code and neighbor can either be a destination or a random other node. If we have already exhausted our K stops with the element being the Kth stop, then we shouldn’t process and update (possibly) the minimum distance for neighbor. This would violate our maximum K stops condition in that case.

As it turns out, I solved this problem originally using Dynamic Programming and it took around 165ms to run on the LeetCode platform. I ran using BFS and it was blazing fast with just 45ms to execute. Motivation enough to write this article for you guys.

I hope you were able to derive some benefit from this article on Breadth First Search and some of its applications. The major focus was to showcase its application to shortest paths in a weighted graph under some constraints ?.