Algorithmic Thinking: A Problem-Based Introduction by Daniel Zingaro
Author:Daniel Zingaro
Language: eng
Format: mobi, epub
ISBN: 9781718500815
Publisher: No Starch Press, Inc.
Published: 2021-11-15T00:00:00+00:00
As we discovered in âShortest Paths in Weighted Graphs,â the shortest path from cell 1 to cell 4 is 17, but the shortest path from cell 4 to cell 1 is 36.
The shortest path from cell 1 to cell 4 uses the edges 1 â 3, 3 â 2, and 2 â 4. If we intend on starting Dijkstraâs algorithm from cell 4, then we need it to find edges 4 â 2, 2 â 3, and 3 â 1. Each of these edges is the reverse of an edge in the original graph. Figure 5-2 shows the reversed graph.
Figure 5-2: Mice Maze reversed graph
Now we can run Dijkstraâs algorithmâjust one invocation of it!âfrom cell 4 to recover shortest paths to all nodes.
In terms of implementation, weâd need to produce the reversed graph instead of the original graph. This can be done in the main function (Listing 5-1), when reading the graph.
Instead of:
e->to_cell = to_cell;
e->length = length;
e->next = adj_list[from_cell];
adj_list[from_cell] = e;
we want this:
e->to_cell = from_cell;
e->length = length;
e->next = adj_list[to_cell];
adj_list[to_cell] = e;
That is, the edge now points to from_cell, and it gets added to the linked list for to_cell. If you make this change and adapt the code so that it invokes Dijkstraâs algorithm just once (from the exit cell), youâll end up with a much faster program. Give it a try!
Download
Algorithmic Thinking: A Problem-Based Introduction by Daniel Zingaro.epub
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(8304)
Test-Driven Development with Java by Alan Mellor(6752)
Data Augmentation with Python by Duc Haba(6666)
Principles of Data Fabric by Sonia Mezzetta(6419)
Learn Blender Simulations the Right Way by Stephen Pearson(6313)
Microservices with Spring Boot 3 and Spring Cloud by Magnus Larsson(6188)
Hadoop in Practice by Alex Holmes(5962)
Jquery UI in Action : Master the concepts Of Jquery UI: A Step By Step Approach by ANMOL GOYAL(5809)
RPA Solution Architect's Handbook by Sachin Sahgal(5588)
Big Data Analysis with Python by Ivan Marin(5375)
The Infinite Retina by Robert Scoble Irena Cronin(5276)
Life 3.0: Being Human in the Age of Artificial Intelligence by Tegmark Max(5152)
Pretrain Vision and Large Language Models in Python by Emily Webber(4345)
Infrastructure as Code for Beginners by Russ McKendrick(4104)
Functional Programming in JavaScript by Mantyla Dan(4040)
The Age of Surveillance Capitalism by Shoshana Zuboff(3959)
WordPress Plugin Development Cookbook by Yannick Lefebvre(3819)
Embracing Microservices Design by Ovais Mehboob Ahmed Khan Nabil Siddiqui and Timothy Oleson(3621)
Applied Machine Learning for Healthcare and Life Sciences Using AWS by Ujjwal Ratan(3597)
