ScholarQuill logoScholarQuillUniversity Notes
  • Notes
  • Past Papers
  • Blogs
  • Todo
Login
ScholarQuill logoScholarQuillUniversity Notes
Login
NotesPast PapersBlogsTodo
More
SubjectsDiscussionCGPA CalculatorGPA CalculatorStudent PortalCourse Outline
About
About usPrivacy PolicyReportContact
Notes
Past Papers
Blogs
Todo
Analytics
    Current Subject
    🧩
    Analysis of Algorithms
    COMP4121
    Progress0 / 28 topics
    Topics
    1. Introduction2. Role of Algorithms in Computing3. Analysis on Nature of Input and Size of Input4. Asymptotic Notations5. Big-O Notation6. Big-Ω Notation7. Big-Θ Notation8. Little-o Notation9. Little-ω Notation10. Sorting Algorithm Analysis11. Loop Invariants12. Recursion and Recurrence Relations13. Algorithm Design Techniques14. Brute Force Approach15. Divide-and-Conquer Approach16. Merge Sort17. Quick Sort18. Greedy Approach19. Dynamic Programming20. Elements of Dynamic Programming21. Search Trees22. Heaps23. Hashing24. Graph Algorithms25. Shortest Paths26. Sparse Graphs27. String Matching28. Introduction to Complexity Classes
    COMP4121›Graph Algorithms
    Analysis of AlgorithmsTopic 24 of 28

    Graph Algorithms

    9 minread
    1,487words
    Intermediatelevel

    Graph Algorithms

    Graphs are mathematical structures used to represent relationships between objects. A graph consists of a set of vertices (or nodes) and a set of edges that connect pairs of vertices. Graphs are widely used in computer science to model networks such as social networks, computer networks, transportation systems, and more.

    Graph algorithms are used to solve a wide range of problems involving graphs, such as finding the shortest path, searching for cycles, and determining connectivity.

    Types of Graphs

    1. Directed vs. Undirected Graphs:

      • Directed Graph (Digraph): Edges have a direction, represented as an ordered pair (u,v)(u, v)(u,v), where uuu is the source vertex, and vvv is the destination vertex.
      • Undirected Graph: Edges have no direction, represented as an unordered pair {u,v}\{u, v\}{u,v}, where uuu and vvv are connected.
    2. Weighted vs. Unweighted Graphs:

      • Weighted Graph: Each edge has an associated weight or cost.
      • Unweighted Graph: Edges have no associated weight; all edges are treated equally.
    3. Connected vs. Disconnected Graphs:

      • Connected Graph: There is a path between any pair of vertices.
      • Disconnected Graph: Some vertices are not connected by any path.
    4. Cyclic vs. Acyclic Graphs:

      • Cyclic Graph: Contains at least one cycle, i.e., a path that starts and ends at the same vertex.
      • Acyclic Graph: Does not contain any cycles.
      • DAG (Directed Acyclic Graph): A directed graph with no cycles.

    Graph Traversal Algorithms

    Graph traversal is the process of visiting all the vertices in a graph. There are two primary methods of traversal:

    1. Depth-First Search (DFS):

      • DFS explores as far as possible along each branch before backtracking.
      • It uses a stack (either explicitly or via recursion) to keep track of vertices to be explored.
      • Time Complexity: O(V+E)O(V + E)O(V+E), where VVV is the number of vertices, and EEE is the number of edges.
      • Space Complexity: O(V)O(V)O(V) (for the recursion stack or an explicit stack).

      Steps for DFS:

      1. Start from a vertex and visit it.
      2. Visit an adjacent unvisited vertex, pushing it onto the stack (or calling it recursively).
      3. Repeat the process until all vertices reachable from the starting vertex are visited.

      Applications:

      • Pathfinding in a maze or graph.
      • Topological sorting in DAGs.
      • Cycle detection in graphs.
    2. Breadth-First Search (BFS):

      • BFS explores the graph level by level, starting from a source vertex and visiting all of its neighbors before moving to the next level.
      • It uses a queue to keep track of vertices to be explored.
      • Time Complexity: O(V+E)O(V + E)O(V+E).
      • Space Complexity: O(V)O(V)O(V).

      Steps for BFS:

      1. Start from the source vertex and visit it.
      2. Enqueue all its neighbors to the queue.
      3. Repeat the process for each vertex in the queue, visiting all unvisited neighbors.

      Applications:

      • Shortest path in an unweighted graph.
      • Finding connected components in an undirected graph.
      • Web crawling (finding all linked pages).

    Shortest Path Algorithms

    1. Dijkstra's Algorithm (For Weighted Graphs with Non-negative Weights):

      • Dijkstra's algorithm finds the shortest path from a source vertex to all other vertices in a graph with non-negative weights.
      • It uses a greedy approach, selecting the vertex with the smallest tentative distance and updating the distances to its neighbors.

      Steps for Dijkstra’s Algorithm:

      1. Initialize the distance to the source vertex as 0 and all other distances as infinity.
      2. Set the source vertex as the current vertex and mark it as visited.
      3. For each neighbor of the current vertex, update its distance if a shorter path is found.
      4. Choose the unvisited vertex with the smallest distance and repeat the process until all vertices are visited.

      Time Complexity: O(V2)O(V^2)O(V2) with a simple array or O(Vlog⁡V+Elog⁡V)O(V \log V + E \log V)O(VlogV+ElogV) using a priority queue (min-heap). Applications:

      • Routing algorithms in networks.
      • GPS navigation systems.
    2. Bellman-Ford Algorithm:

      • The Bellman-Ford algorithm is used to find the shortest path in a graph that may contain negative weight edges.
      • Unlike Dijkstra’s algorithm, Bellman-Ford can handle negative weight edges and detect negative weight cycles.

      Steps for Bellman-Ford:

      1. Initialize the distance to the source vertex as 0 and all other distances as infinity.
      2. Relax all edges V−1V-1V−1 times, where VVV is the number of vertices.
      3. Check for negative weight cycles by examining if any distance can still be relaxed after V−1V-1V−1 iterations.

      Time Complexity: O(VE)O(VE)O(VE). Applications:

      • Graphs with negative edge weights.
      • Detecting negative weight cycles.
    3. Floyd-Warshall Algorithm (All-Pairs Shortest Path):

      • Floyd-Warshall computes the shortest paths between all pairs of vertices in a graph.
      • It works on both weighted and unweighted graphs, and it can handle negative edge weights as well, but not negative weight cycles.

      Time Complexity: O(V3)O(V^3)O(V3). Applications:

      • Finding shortest paths between all pairs of vertices.
      • Network routing.

    Minimum Spanning Tree (MST)

    A minimum spanning tree is a subset of the edges in a weighted graph that connects all vertices without any cycles and with the minimum possible total edge weight.

    1. Prim's Algorithm:

      • Prim’s algorithm is a greedy algorithm that finds the MST by starting from an arbitrary vertex and repeatedly adding the shortest edge that connects a vertex in the MST to a vertex outside the MST.
      • It uses a priority queue to efficiently select the next edge with the smallest weight.

      Time Complexity: O(Elog⁡V)O(E \log V)O(ElogV) using a priority queue. Applications:

      • Designing efficient networks (e.g., computer networks, road networks).
    2. Kruskal's Algorithm:

      • Kruskal’s algorithm finds the MST by sorting all edges by weight and adding the shortest edge to the MST if it doesn’t form a cycle, using the Union-Find data structure to detect cycles.
      • It is efficient when the graph is sparse, and it works by considering edges in order of increasing weight.

      Time Complexity: O(Elog⁡E)O(E \log E)O(ElogE) or O(Elog⁡V)O(E \log V)O(ElogV) (since sorting edges dominates). Applications:

      • Similar to Prim's, used in network design and clustering problems.

    Topological Sorting

    Topological sorting is the linear ordering of vertices in a directed acyclic graph (DAG) such that for every directed edge (u,v)(u, v)(u,v), vertex uuu comes before vertex vvv in the ordering.

    • DFS-based Approach: Perform a depth-first search (DFS) on the DAG. The topological sort is the reverse of the postorder traversal.
    • Kahn's Algorithm: Use in-degree (number of incoming edges) of vertices and process vertices with in-degree 0, removing them and updating the in-degrees of their neighbors.

    Time Complexity: O(V+E)O(V + E)O(V+E) for both approaches.

    Applications:

    • Task scheduling and project planning.
    • Compiler design (dependency resolution).

    Strongly Connected Components (SCC)

    A strongly connected component (SCC) of a directed graph is a maximal subset of vertices such that there is a directed path from every vertex to every other vertex in the subset.

    1. Kosaraju’s Algorithm:

      • Kosaraju’s algorithm finds all SCCs in a graph in two passes of DFS.
      • First, perform a DFS on the original graph, storing the vertices in postorder. Then, reverse all edges and perform DFS again in the order of the vertices in postorder.

      Time Complexity: O(V+E)O(V + E)O(V+E).

    2. Tarjan’s Algorithm:

      • Tarjan’s algorithm also finds SCCs in a single pass of DFS and uses a stack to keep track of the current path.

      Time Complexity: O(V+E)O(V + E)O(V+E).


    Summary

    Graph algorithms are fundamental tools for solving a wide range of problems in computer science, such as:

    • Traversal (DFS, BFS) for visiting nodes.
    • Shortest path algorithms (Dijkstra, Bellman-Ford, Floyd-Warshall).
    • Minimum spanning trees (Prim's, Kruskal's).
    • Topological sorting and strongly connected components for directed graphs.

    By utilizing these algorithms efficiently, we can solve problems like pathfinding, network design, scheduling, and more.

    Previous topic 23
    Hashing
    Next topic 25
    Shortest Paths

    Past Papers

    Open this section to load past papers

    Click on Show Past Papers to see past papers.
    On This Page
      Reading Stats
      Est. reading time9 min
      Word count1,487
      Code examples0
      DifficultyIntermediate