Forums
New posts
Search forums
News
Security News
Technology News
Giveaways
Giveaways, Promotions and Contests
Discounts & Deals
Reviews
Users Reviews
Video Reviews
Support
Windows Malware Removal Help & Support
Inactive Support Threads
Mac Malware Removal Help & Support
Mobile Malware Removal Help & Support
Blog
Log in
Register
What's new
Search
Search titles only
By:
Search titles only
By:
Reply to thread
Menu
Install the app
Install
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Forums
Guides
Programming Guides & Questions
Need help with a problem in the implementation of Dijkstra's algorithm.
Message
<blockquote data-quote="quentisa" data-source="post: 1049594" data-attributes="member: 98424"><p>I'm presently working on implementing Dijkstra's method for a graph traversal problem, however I'm having trouble figuring out an error. The method succeeds, but the shortest path it generates is inaccurate.</p><p>Here's a piece of code that computes the shortest distances:</p><p>[CODE]distances = [float('inf')] * num_nodes</p><p>distances[source] = 0</p><p>visited = [False] * num_nodes</p><p></p><p>while True:</p><p> min_distance = float('inf')</p><p> min_node = -1</p><p></p><p> for node in range(num_nodes):</p><p> if not visited[node] and distances[node] < min_distance:</p><p> min_distance = distances[node]</p><p> min_node = node</p><p></p><p> if min_node == -1:</p><p> break</p><p></p><p> visited[min_node] = True</p><p></p><p> for neighbor in graph[min_node]:</p><p> new_distance = distances[min_node] + graph[min_node][neighbor]</p><p> if new_distance < distances[neighbor]:</p><p> distances[neighbor] = new_distance[/CODE]</p><p>I went through the code and double-checked my graph representation to ensure that the edge weights are not negative, and I read the <a href="https://www.scaler.com/topics/data-structures/dijkstra-algorithm/" target="_blank">scaler</a> article for Dijkstra's algorithm to get a check. Despite the fact that the method ends, the distances calculated are incorrect for some nodes.</p><p></p><p>I believe there is a problem with the termination condition or the way I label nodes as visited. Could the loop condition or the method I update the visited list have a logical flaw? I can't figure out what's causing the wrong distances.</p><p></p><p>I would highly appreciate your aid if you have any thoughts or recommendations on how to address this mistake and assure the right shortest path lengths. Thank you ahead of time!"</p></blockquote><p></p>
[QUOTE="quentisa, post: 1049594, member: 98424"] I'm presently working on implementing Dijkstra's method for a graph traversal problem, however I'm having trouble figuring out an error. The method succeeds, but the shortest path it generates is inaccurate. Here's a piece of code that computes the shortest distances: [CODE]distances = [float('inf')] * num_nodes distances[source] = 0 visited = [False] * num_nodes while True: min_distance = float('inf') min_node = -1 for node in range(num_nodes): if not visited[node] and distances[node] < min_distance: min_distance = distances[node] min_node = node if min_node == -1: break visited[min_node] = True for neighbor in graph[min_node]: new_distance = distances[min_node] + graph[min_node][neighbor] if new_distance < distances[neighbor]: distances[neighbor] = new_distance[/CODE] I went through the code and double-checked my graph representation to ensure that the edge weights are not negative, and I read the [URL='https://www.scaler.com/topics/data-structures/dijkstra-algorithm/']scaler[/URL] article for Dijkstra's algorithm to get a check. Despite the fact that the method ends, the distances calculated are incorrect for some nodes. I believe there is a problem with the termination condition or the way I label nodes as visited. Could the loop condition or the method I update the visited list have a logical flaw? I can't figure out what's causing the wrong distances. I would highly appreciate your aid if you have any thoughts or recommendations on how to address this mistake and assure the right shortest path lengths. Thank you ahead of time!" [/QUOTE]
Insert quotes…
Verification
Post reply
Top