Graphs

#programming #graphs #data-structures

A graph is a data structure that consists of a set of nodes (or vertices) and a set of edges that relate these nodes

Formally, a graph G is represented as G=(V,E) where

Directed and undirected

Trees are a special case of graph; in trees, there are no cycles.

General Terminology

Important

No. of edges in a complete directed graph with n vertices?

  • Since a complete graph has all connections, this means that two nodes have two edges between them, going both directions.
  • Number of edges = 2 \times\ { #nC_} {2} = n\times(n-1)
Important

No. of edges in a complete undirected graph with n vertices?

  • nC2=n×(n1)/2

Graph Implementation

Adjacency Matrix

Adjacency List

Traversal

Breadth-first

Algorithm

Depth-first

Algorithm

Undirected graphs

Connectivity

Directed graphs

Terminologies

Connectivity

Transitive closure

In any graph G, it's transitive close G basically tells you if two nodes are connected by a path.
If a certain node is reachable from another node (maybe through some intermediate nodes), then G has an edge between those two nodes.
Therefore, G has

Transitive closure matrix : An extension to adjacency matrix.

This can be expanded for a digraph as follows

Algorithms to construct transitive closure


Relations

Relation between V and E

  • A connected graph with no cycles obeys E=V1; This is the case of a tree
  • If a graph has EV, then it must contain atleast one cycle
  • If a connected graph has E=V then it contains exactly one cycle and is called a unicyclic graph.

Powered by Forestry.md