In this tutorial, you will learn the Graph data structure, its operations and basic implementation example with Java

What is Graph Data Structure?

Graph is a non-linear data structure consisting of a finite set of nodes/vertices and a set of finite edges which connect 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

Representations

  • 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