Skip to content

Commit

Permalink
Merge pull request #71 from hmdsefi/dev
Browse files Browse the repository at this point in the history
update main readme
  • Loading branch information
hmdsefi authored Feb 17, 2024
2 parents 801f294 + d52aa9b commit b4feb78
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 16 deletions.
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,16 @@ educational purposes and practical applications.</p>
* [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
Expand Down
8 changes: 4 additions & 4 deletions path/bellman-ford.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
16 changes: 9 additions & 7 deletions path/dijkstra.md
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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.
Expand All @@ -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:
Expand All @@ -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.
2 changes: 1 addition & 1 deletion path/floyd-warshall.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

0 comments on commit b4feb78

Please sign in to comment.