From d52aa9b00145f0a4bce8cc647e52b4bd13ae5fd9 Mon Sep 17 00:00:00 2001 From: Hamed Yousefi Date: Sat, 17 Feb 2024 20:49:27 +0330 Subject: [PATCH] update main readme --- README.md | 12 ++++++++---- path/bellman-ford.md | 8 ++++---- path/dijkstra.md | 16 +++++++++------- path/floyd-warshall.md | 2 +- 4 files changed, 22 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 2f2881f..7bf262d 100644 --- a/README.md +++ b/README.md @@ -19,12 +19,16 @@ educational purposes and practical applications.

* [Install](#Install) * [How to Use](#How-to-Use) * [Graph](#Graph) - * [Directed](#Directed) - * [Acyclic](#Acyclic) - * [Undirected](#Undirected) - * [Weighted](#Weighted) + * [Directed](#Directed) + * [Acyclic](#Acyclic) + * [Undirected](#Undirected) + * [Weighted](#Weighted) * [Traverse](#Traverse) * [Connectivity](https://github.com/hmdsefi/gograph/tree/master/connectivity#gograph---connectivity) + * [Shortest Path]() + * [Dijkstra](https://github.com/hmdsefi/gograph/blob/master/path/dijkstra.md) + * [Bellman-Ford](https://github.com/hmdsefi/gograph/blob/master/path/bellman-ford.md) + * [Floyd-Warshall](https://github.com/hmdsefi/gograph/blob/master/path/floyd-warshall.md) * [License](#License) ## Install diff --git a/path/bellman-ford.md b/path/bellman-ford.md index 625ec72..4897a7b 100644 --- a/path/bellman-ford.md +++ b/path/bellman-ford.md @@ -10,22 +10,22 @@ Here's a step-by-step explanation of how the Bellman-Ford algorithm works: 1. **Initialization:** Start by setting the distance of the source vertex to itself as 0, and the distance of all other vertices to infinity. -2. **Relaxation:** Iterate through all edges in the graph |V| - 1 times, where |V| is +2. **Relaxation:** Iterate through all edges in the graph `|V| - 1` times, where `|V|` is the number of vertices. In each iteration, attempt to improve the shortest path estimates for all vertices. This is done by relaxing each edge: if the distance to the destination vertex through the current edge is shorter than the current estimate, update the estimate with the shorter distance. -3. **Detection of Negative Cycles:** After the |V| - 1 iterations, perform an additional iteration. +3. **Detection of Negative Cycles:** After the `|V| - 1` iterations, perform an additional iteration. If during this iteration, any of the distances are further reduced, it indicates the presence of a negative weight cycle in the graph. This is because if a vertex's distance can still be improved -after |V| - 1 iterations, it means there's a negative weight cycle that can be traversed indefinitely +after `|V| - 1` iterations, it means there's a negative weight cycle that can be traversed indefinitely to reduce the distance further. 4. **Output:** If there is no negative weight cycle, the algorithm outputs the shortest path distances from the source vertex to all other vertices. If there is a negative weight cycle, the algorithm typically returns an indication of this fact. -The time complexity of the Bellman-Ford algorithm is O(V*E), where V is the number of vertices and E +The time complexity of the Bellman-Ford algorithm is `O(V*E)`, where V is the number of vertices and E is the number of edges. This makes it less efficient than algorithms like Dijkstra's algorithm for graphs with non-negative edge weights, but its ability to handle negative weight edges (as long as there are no negative weight cycles) makes it useful in certain scenarios. diff --git a/path/dijkstra.md b/path/dijkstra.md index fe2a0f1..d21a49b 100644 --- a/path/dijkstra.md +++ b/path/dijkstra.md @@ -1,6 +1,6 @@ # gograph ## Shortest Path -### Bellman-Ford +### Dijkstra Dijkstra's algorithm is a graph algorithm used to find the shortest path from a single source vertex to all other vertices in a weighted graph with non-negative edge weights. It was developed by Dutch computer scientist Edsger W. Dijkstra in 1956. @@ -26,7 +26,7 @@ Dijkstra's algorithm guarantees the shortest path from the source vertex to all as long as the graph does not contain negative weight edges. It works efficiently for sparse graphs with non-negative edge weights. -The time complexity of Dijkstra's algorithm is O((V + E) log V), where V is the number of vertices and E is +The time complexity of Dijkstra's algorithm is `O((V + E) log V)`, where V is the number of vertices and E is the number of edges in the graph. This complexity arises from the use of a priority queue to maintain the tentative distances efficiently. If a simple array-based implementation is used to select the minimum distance vertex in each step, the time complexity becomes O(V^2), which is more suitable for dense graphs. @@ -39,9 +39,10 @@ Steps: 4. Relax the distances of its neighboring vertices if a shorter path is found. 5. Mark the selected vertex as visited. -**Time Complexity:** O(V^2), where V is the number of vertices in the graph. This is because finding the -minimum distance vertex in each iteration takes O(V) time, and we perform this process V times. -**Space Complexity:** O(V^2) for storing the graph and distances. +**Time Complexity:** `O(V^2)`, where V is the number of vertices in the graph. This is because finding the +minimum distance vertex in each iteration takes `O(V)` time, and we perform this process V times. + +**Space Complexity:** `O(V^2)` for storing the graph and distances. #### Implementation with Heap Steps: @@ -50,7 +51,8 @@ we use a heap to maintain the priority queue. * The priority queue ensures that the vertex with the smallest tentative distance is efficiently selected in each iteration. -**Time Complexity:** O((V + E) log V), where V is the number of vertices and E is the number of edges in +**Time Complexity:** `O((V + E) log V)`, where V is the number of vertices and E is the number of edges in the graph. This is because each vertex is pushed and popped from the priority queue once, and each edge is relaxed once. -**Space Complexity:** O(V) for storing the priority queue and distances. + +**Space Complexity:** `O(V)` for storing the priority queue and distances. diff --git a/path/floyd-warshall.md b/path/floyd-warshall.md index 52b6afc..e31bb24 100644 --- a/path/floyd-warshall.md +++ b/path/floyd-warshall.md @@ -23,7 +23,7 @@ is an edge, otherwise set the value to infinity. Also, set the diagonal elements pairs of vertices. If there is a negative weight cycle, it might not produce the correct shortest paths, but it can still detect the presence of such cycles. -The time complexity of the Floyd-Warshall algorithm is O(V^3), where V is the number of vertices in the graph. +The time complexity of the Floyd-Warshall algorithm is `O(V^3)`, where V is the number of vertices in the graph. Despite its cubic time complexity, it is often preferred over other algorithms like Bellman-Ford for dense graphs or when the graph has negative weight edges and no negative weight cycles, as it calculates shortest paths between all pairs of vertices in one go. \ No newline at end of file