How the Graphs Were Generated

Total Page:16

File Type:pdf, Size:1020Kb

How the Graphs Were Generated

John Kizhakeparampil

CSCE 4110

Term Project

How the graphs were generated.

The graphs were generated using a c++ code. The 30 complete directed graphs were created using random number generator. The graphs were created in adjacency matrix format. The total number of nodes on the graph were chosen as a random number between 50 to about 1000. Weights between any two nodes were also assigned as a random number between 1 and 5.

Finally, the matrix was written to a file to be able to be read later using file stream. The graphs created are text files.

The first row signifies the number of nodes in the graphs.

The subsequent rows are in the format

Node1, Node2, Edge, Weight

Some explanation about the Dijkstra Code

Below are the steps we used to find the shortest path from a source to all other vertices. 1) We created an array shortest_path that stores info of vertices included in shortest path tree, i.e., whose minimum distance from source is calculated and finalized. Initially, this is empty.

2) We then assign distance value to all vertices in the igraph. All other vertices except the source vertex is assigned infinite distance. 3)Then until shortest_path array doesn't include all vertex in graph we do the following:

We pick a vertex v not present in shortest_path and that has minimum distance value We include that vertex to shortest_path.We also update the distance value of all vertex adjacent to v .For this we iterate through all adjacent vertices of v and then for every adjacent vertex x, if sum of distance value of v(from the source) and weight of edge v-x, is less than the distance value of x, then we update the distance value of vertex x.

Floyd Code:

In the first step We initialize the final distance matrix same as the input graph matrix . Then we update the distance matrix by considering all vertices as intermediate vertex. We pick all the vertices one by one and then update all shortest paths which include the picked vertex as an intermediate vertex in the shortest path.

Let's say we pick vertex number k as an intermediate vertex, then we already have considered vertices {0, 1, 2, .. k-1} as it's intermediate vertices. For every pair (i, j) where I is source vertex and j is destination vertex respectively, there are two possible cases. 1)k may not be an intermediate vertex in shortest path from i to j. In such case we don't modify the distance matrix. 2)If k is an intermediate vertex in shortest path from i to j wee update the value of dist[i][j] = dist[i][k] + dist[k][j].

Dijkstra’s Algorithm Edsger Dijkstra discovered Dijkstra’s algorithm in 1959. This algorithm solves the single-source shortest-path problem by finding the shortest path between a given source vertex and then towards all other vertices. This algorithm works by first finding the path with the lowest total weight between two vertices, j and k. It then uses the fact that a node r, in the path from j to k implies a minimal path from j to r. In the end, we will have the shortest path from a given vertex to all other vertices in the graph. Dijkstra’s algorithm belongs to a class of algorithms known as greedy algorithms. A greedy algorithm makes the decision that seems the most promising at a given time and then never reconsiders that decision. The time complexity of Dijkstra’s algorithm is dependent upon the internal data structures used for implementing the queue and representing the graph. When using an adjacency list to represent the graph and an unordered array to implement the queue the time complexity is O(n2), where n is the number of vertices in the graph.

The number of vertices range from 50 to 1000 in 30 different test graphs for algorithm execution and their CPU times are given below which will give us a detailed performance comparison between Dijkstra’s and Floyd Warshall’s algorithms.

Dijkstra’s algorithm has a running time of O(n2) time.

Is this table correct? Looks good

Directed Graph Number Number of nodes in graph Dijkstra CPU Time (secs)

0 602 4.572

1 602 4.888

2 602 4.364

3 602 4.832

4 249 0.420

5 249 0.504 6 249 0.412

7 249 0.540

8 249 0.444

9 249 0.620

10 249 0.508

11 249 0.540

12 249 0.444

13 249 0.604

14 249 0.536

15 249 0.532

16 249 0.496

17 249 0.468

18 276 0.608

19 276 0.628

20 276 0.600

21 276 0.616

22 276 0.580

23 276 0.596

24 276 0.568

25 276 0.588

26 276 0.540

27 276 0.556

28 276 0.764

29 276 0.552

Dijkstra’s algorithm Graph Diagram Whenever I try to insert a line dotted graph, I do not see any line.

What kind of graph diagram for dijkstra should I use?

I am not sure how we can draw such huge graphs in line dotted form but a directed graph usually looks like below:

Floyd’s Algorithm Stephen Warshall and Robert Floyd independently discovered Floyd’s algorithm in 1962. This algorithm is sometimes referred to as the Warshall-Floyd algorithm or the Roy-Floyd algorithm. The algorithm finds the shortest path between all the vertices of a given graph. This algorithm works by estimating the shortest path between two vertices and further improving that estimate until it is optimum. Consider a graph G, with Vertices V numbered 1 to n. The algorithm first finds the shortest path from i to j, using only vertices 1 to k, where k<=n. Next, using the previous result the algorithm finds the shortest path from i to j, using vertices 1 to k+1. We continue using this method until k=n, at which time we have the shortest path between all vertices. This algorithm has a time complexity of O(n3), where n is the number of vertices in the graph.

Floyd-Warshall algorithm has a running time of O(n3) time.

Is this table also correct? Looks good

Directed Graph Number Number of nodes in graph Floyd CPU Time (secs)

0 602 2.428

1 602 2.588

2 602 2.356 3 602 3.316

4 249 0.372

5 249 0.412

6 249 0.432

7 249 0.376

8 249 0.308

9 249 0.276

10 249 0.328

11 249 0.312

12 249 0.288

13 249 0.304

14 249 0.268

15 249 0.316

16 249 0.276

17 249 0.316

18 276 0.396

19 276 0.400

20 276 0.388

21 276 0.344

22 276 0.392

23 276 0.376

24 276 0.384

25 276 0.400

26 276 0.340 27 276 0.316

28 276 0.364

29 276 0.368

Floyd-Warshall algorithm Graph Diagram

Whenever I try to insert a line dotted graph, I do not see any line.

What kind of graph diagram for Floyd should I use?

Conclusion: Can you read this and add any to it? Looks good

Both Floyd-Warshall and Dijkstra's algorithms can be used to find the shortest path between vertices in graphs. The Floyd-Warshall algorithm finds the shortest path between all vertices and is much easier to implement, while Dijkstra's algorithm finds the shortest path between a single vertex and all other vertices.

According to the experiment tables above, for small values, number of vertices, Dijkstra's algorithm is not worth the effort on implementation. When the number of vertices in a graph increases, the performance of Floyd's algorithm decreases. Therefore, Dijkstra's algorithm is capable when performance is a factor. While, if you will need the shortest path that contains several vertices on the same graph you should consider Dijkstra's algorithm.

Recommended publications