Back to course

Lesson 36: Introduction to Graphs (Nodes, Edges, Types)

Algorithms: From Zero to Hero (A Beginner's Guide)

36. Introduction to Graphs

A Graph is a non-linear data structure used to model relationships between objects. It consists of a set of Vertices (V) (or nodes) and a set of Edges (E) (or connections).

Graph Types

  1. Undirected Graph: Edges have no direction. If A is connected to B, B is also connected to A (e.g., friendships on Facebook).
  2. Directed Graph (Digraph): Edges have a direction. A connection from A to B does not imply a connection from B to A (e.g., Twitter followers).
  3. Weighted Graph: Edges have numerical values (weights) assigned to them, representing cost, distance, or time (e.g., road network distances).
  4. Cyclic vs. Acyclic: A cycle is a path that starts and ends at the same vertex. A graph without cycles is acyclic (like a tree).

Graphs are used to model everything from transportation networks to website links and complex dependencies.