📈

Graphs

A graph is a data structure that consists of a finite set of vertices (or nodes) and a collection of edges connecting these vertices. The edges may or may not have a direction, and they may have weights or labels. Graphs are used to represent relationships and connections between different entities.

Components of a Graph:

  1. Vertices (Nodes):
    • The fundamental entities in a graph.
    • Represented by points in a graph.
  1. Edges:
    • Connections between vertices that represent relationships.
    • An edge can be directed (arrow indicating a one-way connection) or undirected (no direction).

Types of Graphs:

Graph Representation:

Graph Operations:

Applications of Graphs:

Graph Traversal Techniques:

Breadth-First Search (BFS):

Depth-First Search (DFS):

Minimum Spanning Tree (MST):

A Minimum Spanning Tree (MST) of a connected, undirected graph is a tree that spans all the vertices of the graph and has the minimum possible total edge weight. In other words, it is a subset of the edges of the graph that forms a tree and connects all the vertices with the minimum total edge weight.

Key properties of a minimum spanning tree:

  1. Connectivity: It connects all the vertices in the original graph.
  1. Acyclic: It forms a tree, meaning there are no cycles in the tree.
  1. Minimum Weight: The sum of the edge weights in the tree is minimized.

1. Kruskal's Algorithm:

2. Prim's Algorithm:

Difference Between BFS and DFS:

FeatureBreadth-First Search (BFS)Depth-First Search (DFS)
Exploration OrderVisits all neighbors at the current level before moving on to the next level.Explores as far as possible along each branch before backtracking.
Data Structure UsedUses a queue to maintain the order of vertex exploration.Uses a stack (either explicitly or through recursion) to keep track of vertices.
ApplicationOften used for finding the shortest path in unweighted graphs.Commonly used for detecting cycles, topological sorting, and solving maze problems.
CompletenessGuarantees the shortest path in unweighted graphs.Does not guarantee the shortest path.
Order of ExplorationExplores vertices in the order they are enqueued.Explores vertices in the order they are popped from the stack.
Example Use CaseFinding the shortest path between two nodes in an unweighted graph.Detecting cycles in a graph or performing topological sorting.

Difference Between Kruskal’s and Prism’s:

FeatureKruskal's AlgorithmPrim's Algorithm
Algorithm TypeGreedy algorithm that selects edges based on weight without forming cycles.Greedy algorithm that grows the tree from an initial vertex.
OperationWorks by repeatedly adding the smallest edge that doesn't form a cycle.Works by growing the tree from an initial vertex, adding the smallest edge to connect the tree with the rest of the graph.
Edge SelectionChooses edges based on weight without concern for the source or destination vertices.Chooses edges based on weight to connect the current tree with the closest non-tree vertex.
Deterministic OutputMay have multiple valid solutions depending on the order edges are considered.Always produces the same minimum spanning tree for a given starting vertex.
Use CasesSuitable for sparse graphs and scenarios where edge weights are relatively uniform.Suitable for dense graphs and scenarios where there is a clear central location or starting vertex.