Graph is a non-linear data structure consisting of a collection of nodes/vertices and edges G = (V, E)

Each edge connects a pair of nodes

Types of Graphs

  • Undirected graph

  • Directed graph

  • Tree is a connected graph (existing a path between every pair of vertices) with no cycles

Basic operations

  • addEdge, add an edge between two vertices of the graph


  • Adjacency List, every vertex stores a list of adjacent vertices
  • Adjacency Matrix, a two dimensional matrix array in which the rows represent source vertices and columns represent destination vertices

Undirected Graph implementation example with Adjacency List