4.2 KiB
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. TSP is NP, 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.
- Initialize distances:
- Set the distance to the source vertex as 0 and all others as infinity.
- Use a priority queue (min-heap) to extract the vertex with the smallest distance.
- For each neighboring vertex, calculate the tentative distance via the current vertex:
- If the tentative distance is smaller than the known distance, update it.
- Repeat until all vertices are processed.
- Using a priority queue:
O((V + E) \log V)
, whereV
is the number of vertices andE
is the number of edges.
Floyd-Warshall Algorithm
Computes shortest paths between all pairs of nodes in a weighted graph.
- Initialize distances with edge weights.
- Iteratively update distances by considering each node as an intermediate point.
- Handle both positive and negative edge weights (no negative cycles allowed).
- Complexity:
O(V^3)
, whereV
is the number of vertices.
Minimum Spanning Tree Algorithms
Kruskal's Algorithm
Finds the minimum spanning tree (MST) of a graph.
- Sort all edges by weight.
- Add edges to the MST in increasing order of weight.
- Skip edges that form a cycle (using Union-Find data structure :) ).
Prim's Algorithm
Constructs the minimum spanning tree (MST) of a graph.
- Start with an arbitrary node.
- Repeatedly add the smallest edge that connects a node in the MST to a node outside it.
- Continue until all nodes are included.
O(E + V \log V)
using a priority queue, whereV
is the number of vertices andE
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.
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.
Maximum Flow Algorithms
Ford-Fulkerson Algorithm
**Finds the maximum flow in a flow network.
- Initialize the flow to 0.
- While there is an augmenting path, increase the flow along the path.
- Repeat until no augmenting paths remain.
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.
- Start from the source node.
- Use a priority queue to explore the most promising paths first.
- Use a heuristic to guide the search toward the goal.
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.
- Greedy coloring: