Programming - Java Graph All Pair Shortest Path Algorithms (Floyd-Warshall/Johnson) by drifter1

View this thread on steempeak.com
· @drifter1 · (edited)
$10.24
Programming - Java Graph All Pair Shortest Path Algorithms (Floyd-Warshall/Johnson)
<html>
<p><img src="https://www2.hawaii.edu/~janst/311/Notes/Topic-19/algorithm-Floyd-Warshall-Prime.jpg" width="278" height="164"/></p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;Hello its me again drifter1! Today we get into <strong>Java </strong>again talking about the <strong>2 main All Pair Shortest Path Algorithms</strong> that are used in <strong>Graphs</strong>. I will mainly talk about the <strong>Floyd Warshall algorithm </strong>that we will also implement and I will also explain how the <strong>Johnson algorithm</strong> works. You can check out the previous post with the <strong>single-source Bellman-Ford algorithm</strong> <a href="https://steemit.com/programming/@drifter1/programming-java-graph-shortest-path-algorithm-bellman-ford">here</a>! So, without further do let's get started!</p>
<h2>All pair shortest path problem:</h2>
<p>&nbsp;&nbsp;&nbsp;&nbsp;Let's first get into what this problem is all about. We already know that with a single-source algorithm we can find the shortest path from one (source) vertex to any other vertex in the graph. But, in many circumstances we may need to <strong>find the shortest path from any vertex to any other vertex</strong>. This is when a <strong>APSP algorithm</strong> is needed and becomes useful.</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;Think about a network where the computers are connected together randomly like a graph. Suppose one of the computers wants to send a message to another, but this message needs to get to it's destination in the quickest time possible. Here we can use a single-source algorithm to find the shortest path to the destination. But, what if another computer wants to send a message to the same computer? Well, then we will have to run a single-source algorithm again. To eliminate this cost we can have one of the computers (that will be elected during a voting-like process) calculate the shortest paths from all to all vertices using an all pair shortest path algorithm and send this information to all the other computers. This way all the computers know which is the fastest path to any other computer. Using this concept we need to make sure that the path is existent and that no computer in between has been shut down and/or is not available. This topic has to do with Distributed and Parallel Systems that I will cover some time in the near future in our Networking category! :)</p>
<p><br></p>
<h2>Floyd Warshall Algorithm:</h2>
<p>&nbsp;&nbsp;&nbsp;&nbsp;The Floyd-Warshall algorithm is the <strong>simplest All Pair Shortest Path Algorithm</strong> that you can implement. The algorithm works with <strong>positive and negative Edge weights</strong>, but <strong>can't detect negative weight circles </strong>and so doesn't work with them at all. This algorithm <strong>compares all possible paths through the graph between each pair of vertices </strong>and so has a O(V^3) complexity that makes it <strong>slow with a large number of vertices</strong>.</p>
<p><strong>Pseudocode </strong>from <a href="https://en.wikipedia.org/wiki/Floyd%E2%80%93Warshall_algorithm">wikipedia</a>:</p>
<p><img src="https://s17.postimg.org/k122wop27/Screenshot_1.jpg" width="602" height="226"/>&nbsp;</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;You can see that we have to use a <strong>matrix to store the distances calculated</strong> and first initialize the matrix to the adjacency matrix of the graph and then check all the possible paths with 3 "stacked" for loops! We will use the same pseudocode, but tweak it a little bit to suit our needs.</p>
<p><br></p>
<h2>Floyd Warshall Java Implementation:</h2>
<p>&nbsp;&nbsp;&nbsp;&nbsp;To implement this Algorithm we will use the <strong>adjacency matrix implementation</strong> of our basic graph. We will <strong>change the boolean adjacency matrix to an float matrix</strong> to<strong> store the weights and initialize these weights to infinity</strong> (or the maximum float number). We then first initialize the <strong>distance matrix of the algorithm to equal the adjacency matrix</strong>. We then<strong> loop through all vertices one by one</strong> <strong>in a triple for loop </strong>and check the condition <em><strong>dist(i, k) + dist(k, j) &lt; dist(i, j)</strong></em> and set the <strong>dist(i, j) equal to the left part if it's true</strong>. Printing is pretty simple, and to get rid of the giant floating number I will print INF if the distance is equal to this giant floating number that we called infinity.</p>
<p>So, our <strong>Graph class </strong>looks like this:</p>
<p><img src="https://s17.postimg.org/b7b6ff9rj/graph.jpg" width="407" height="800"/></p>
<p><br></p>
<p>The <strong>TestGraphs class</strong> that also contains the <strong>Algorithm </strong>looks like this:</p>
<p><img src="https://s17.postimg.org/mwf63dt0f/testgraphs_and_algorithm.jpg" width="421" height="800"/></p>
<p><br></p>
<p>Running the last class we will see the <strong>Console </strong>print out the results as following:</p>
<p><img src="https://s17.postimg.org/mjnrx730v/console.jpg" width="351" height="200"/></p>
<p><br></p>
<p>You can get the <strong>codes </strong>in the following <strong>pastebin </strong>links:</p>
<p><a href="https://pastebin.com/5bBzBV2j">Graph</a></p>
<p><a href="https://pastebin.com/YCrvE5mi">TestGraphs/Algorithm</a></p>
<p>Don't forget to remove the package declaration or put the classes inside of a package with that name.</p>
<h2><br></h2>
<h2>Johnson Algorithm:</h2>
<p>&nbsp;&nbsp;&nbsp;&nbsp;So, what is Johnson's Algorithm? Well <strong>Johnson's Algorithm combines the Dijkstra and Bellman-Ford Algorithms </strong>for Single-Source Shortest Paths to solve the All-Pair Shortest Path Problem. &nbsp;The algorithm<strong> first adds a new Node/Vertex to the Graph that is connected to all the others with zero-weight</strong>. It then uses the<strong> Bellman-Ford Algorithm on that newly added node to find the shortest paths from this node</strong> (actually from all the other nodes, because of the zero-weight)<strong> to all the other nodes</strong>, by <strong>also detecting negative-weight circles</strong> that will make the algorithm terminate. The algorithm then <strong>re-weights the original Graph by using the calculated distances</strong> (it removes the negative weights to prepare the graph for Dijkstra) and finally, <strong>after removing the added vertex</strong>, uses <strong>Dijkstra's algorithm to find the shortest paths from each node to every other node in the re-weighted graph</strong>.</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;This algorithm is <strong>slightly faster then the Floyd-Warshall algorithm</strong> and also detects negative circles, something that the first couldn't do. The problem is that this algorithm depends on the other two algorithms and so the implementation becomes much more complex. We already talked about Dijkstra and Bellman-Ford, but the implementation I made was simple so that everybody can understand it. This means that if we put this algorithms in use (after tweaking them a little bit) with Johnson's algorithm we will not get the best possible performance!</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;I will not implement the algorithm and leave it to you. You already know what the algorithm does and I also implemented the two algorithm that this algorithm needs in previous posts. So, if you find a pseudocode online you can actually make it happen pretty fast. Have fun! :P</p>
<p><br></p>
<p>And this is actually it for today!</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;Next time in Java Graphs we will talk about the Maximum Flow problem and the Ford-Fulkerson algorithm that is mainly used to solve it!</p>
<p>Bye!</p>
</html>
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 14 others
properties (23)
post_id18,172,577
authordrifter1
permlinkprogramming-java-graph-all-pair-shortest-path-algorithms-floyd-warshall-johnson
categoryprogramming
json_metadata"{"app": "steemit/0.1", "format": "html", "links": ["https://steemit.com/programming/@drifter1/programming-java-graph-shortest-path-algorithm-bellman-ford", "https://en.wikipedia.org/wiki/Floyd%E2%80%93Warshall_algorithm", "https://pastebin.com/5bBzBV2j", "https://pastebin.com/YCrvE5mi"], "image": ["https://www2.hawaii.edu/~janst/311/Notes/Topic-19/algorithm-Floyd-Warshall-Prime.jpg"], "tags": ["programming", "java", "graph", "floydwarshall", "apsp"]}"
created2017-11-20 00:00:36
last_update2017-11-20 00:07:00
depth0
children0
net_rshares4,488,656,269,915
last_payout2017-11-27 00:00:36
cashout_time1969-12-31 23:59:59
total_payout_value7.831 SBD
curator_payout_value2.407 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length7,633
author_reputation59,186,440,518,630
root_title"Programming - Java Graph All Pair Shortest Path Algorithms (Floyd-Warshall/Johnson)"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
author_curate_reward""
vote details (78)