Log In Sign Up
Day 18

Day 18: Introduction to Graphs

18/60 Days

Introduction to Graphs #

Welcome to Day 18 of our 60 Days of Coding Algorithm Challenge! Today, we’ll dive into the world of graphs, a versatile and powerful data structure used to represent relationships between entities.

What is a Graph? #

A graph is a non-linear data structure consisting of nodes (or vertices) and edges. The nodes are the entities we want to represent, and the edges are the relationships or connections between these entities.

Formally, a graph G is an ordered pair of a set V of vertices and a set E of edges, written as G = (V, E).

Basic Graph Terminology #

  1. Vertex (Node): A fundamental unit of which graphs are formed.
  2. Edge: A connection between two vertices in a graph.
  3. Adjacent Vertices: Two vertices are said to be adjacent if there’s an edge connecting them.
  4. Degree of a Vertex: The number of edges connected to a vertex.
  5. Path: A sequence of vertices where each adjacent pair is connected by an edge.
  6. Cycle: A path that starts and ends at the same vertex.

Types of Graphs #

  1. Undirected Graph: Edges have no direction.
  2. Directed Graph (Digraph): Edges have directions.
  3. Weighted Graph: Edges have weights or costs associated with …