158 lines
4.2 KiB
Markdown
158 lines
4.2 KiB
Markdown
---
|
|
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
|
|
|
|
## 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.
|
|
|
|

|
|
### 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.
|
|
|
|

|
|
|
|
---
|
|
|
|
## 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.
|
|

|
|
|
|
|
|
### 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.
|
|
|
|

|
|
|
|
---
|
|
|
|
## 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)$.
|
|
|
|

|
|
|
|
|
|
---
|
|
|
|
### 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)$.
|
|

|
|
|
|

|
|
|
|
---
|
|
|
|
## 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)$.
|
|

|
|
|
|
---
|
|
|
|
## 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})$.
|
|

|
|
---
|
|
|
|
### Edmonds-Karp Algorithm
|
|
|
|
A refinement of the Ford-Fulkerson algorithm that uses BFS to find augmenting paths.
|
|
|
|
- **Complexity**: $O(V \cdot E^2)$.
|
|
|
|

|
|
|
|
|
|
---
|
|
|
|
## 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.
|
|

|
|
---
|
|
|
|
## 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.
|
|
|
|
---
|