Notes/Advanced Algorithms/Graph Algorithms.md

158 lines
4.2 KiB
Markdown
Raw Permalink Normal View History

2024-12-07 21:07:38 +01:00
---
type: theoretical
---
Can be utilized in algorithms. Here's a rundown of a couple of popular graph algorithms along with their use case and [Complexity](Complexity.md). TSP is [NP](P%20vs.%20NP.md), but it can be approximated/solved using a graph algorithm!
![](graph-algorithms-infographic.gif)
# Graph Algorithms
## Shortest Path Algorithms
### Dijkstra's Algorithm
Finds the shortest path from a source vertex to all other vertices in a graph with non-negative edge weights.
1. Initialize distances:
- Set the distance to the source vertex as 0 and all others as infinity.
2. Use a priority queue (min-heap) to extract the vertex with the smallest distance.
3. For each neighboring vertex, calculate the tentative distance via the current vertex:
- If the tentative distance is smaller than the known distance, update it.
4. Repeat until all vertices are processed.
- Using a priority queue: $O((V + E) \log V)$, where $V$ is the number of vertices and $E$ is the number of edges.
![](Dijkstra01.gif)
### Floyd-Warshall Algorithm
Computes shortest paths between all pairs of nodes in a weighted graph.
1. Initialize distances with edge weights.
2. Iteratively update distances by considering each node as an intermediate point.
3. Handle both positive and negative edge weights (no negative cycles allowed).
- **Complexity**: $O(V^3)$, where $V$ is the number of vertices.
![Floyd-Warshall Algorithm](218610439-933d939c-a9e1-489d-a0a2-da8cf7bf51c1.gif)
---
## Minimum Spanning Tree Algorithms
### Kruskal's Algorithm
Finds the minimum spanning tree (MST) of a graph.
1. Sort all edges by weight.
2. Add edges to the MST in increasing order of weight.
3. Skip edges that form a cycle (using Union-Find data structure :) ).
- **Complexity**: $O(E \log E)$, where $E$ is the number of edges.
![](Animation%20of%20Kruskal's%20Algorithm.gif)
### Prim's Algorithm
Constructs the minimum spanning tree (MST) of a graph.
1. Start with an arbitrary node.
2. Repeatedly add the smallest edge that connects a node in the MST to a node outside it.
3. Continue until all nodes are included.
- $O(E + V \log V)$ using a priority queue, where $V$ is the number of vertices and $E$ is the number of edges.
![Prim's Algorithm](4e486f5e-8437-4e2b-8964-5d5860208502_1650942396.093046.gif)
---
## Graph Traversal Algorithms
### Depth-First Search (DFS)
Explores as far as possible along each branch before backtracking.
- Detects cycles.
- Topological sorting.
- Pathfinding in mazes.
- **Complexity**: $O(V + E)$.
![](1_WR4AtjT_nhwSOtAW99Yd5g.gif)
---
### Breadth-First Search (BFS)
explores all neighbors at the current depth before moving to the next level.
- Finding shortest paths in unweighted graphs.
- Level-order traversal of trees.
- **Complexity**: $O(V + E)$.
![](bfs.gif)
![](dfs-vs-bfs.gif)
---
## Topological Sorting
Arranges the vertices of a directed acyclic graph (DAG) in a linear order such that for every directed edge $(u, v)$, $u$ comes before $v$.
- Perform a DFS and use a stack to store the vertices in reverse order of completion.
- **Complexity**: $O(V + E)$.
![](anim.gif)
---
## Maximum Flow Algorithms
### Ford-Fulkerson Algorithm
**Finds the maximum flow in a flow network.
1. Initialize the flow to 0.
2. While there is an augmenting path, increase the flow along the path.
3. Repeat until no augmenting paths remain.
- **Complexity**: $O(E \cdot \text{max\_flow})$.
![](FordFulkerson.gif)
---
### Edmonds-Karp Algorithm
A refinement of the Ford-Fulkerson algorithm that uses BFS to find augmenting paths.
- **Complexity**: $O(V \cdot E^2)$.
![](56e7f380-cd33-11eb-90b2-658e8d102a95.gif)
---
## Grid-Based Algorithms
### A* Algorithm
Finds the shortest path in a weighted grid using a heuristic.
1. Start from the source node.
2. Use a priority queue to explore the most promising paths first.
3. Use a heuristic to guide the search toward the goal.
- **Complexity**: Depends on the heuristic but generally better than BFS for weighted grids.
![](A.gif)
---
## Graph Coloring
Assigns colors to vertices such that no two adjacent vertices share the same color.
- **Algorithms**:
- Greedy coloring: $O(V^2)$.
- Backtracking for exact coloring.
---