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
is a non-empty finite set of nodes or vertices is a set of edges (represented as pairs of vertices)
Directed and undirected
- A directed graph (digraph) is where each edge has a direction, ie it connects vertices
and , in that order, and does not connect - Essentially means that each element in
is ordered ( )
- Essentially means that each element in
- An undirected graph is represented by just a connection and no direction; bi-directional connections are assumed.
- Essentially means that each element in
is unordered ( )
- Essentially means that each element in
Trees are a special case of graph; in trees, there are no cycles.
General Terminology
- Adjacent nodes : Two nodes
and are called adjacent if they are connected by an edge, Edge is called incident with and , and and are called endpoints of . - Degree of a vertex : Number of edges incident on it. A loop contributes twice to the degree.
- Pendant vertex : Degree = 1
- Isolated vertex : Degree = 0
- Path : A sequence of vertices that connect two nodes
- Complete graph : A graph in which each node is directly connected to each other node
- Weighted graph : A graph in which each edge carries a "weight"
No. of edges in a complete directed graph with
- Since a complete graph has all connections, this means that two nodes have two edges between them, going both directions.
- Number of edges
No. of edges in a complete undirected graph with
Graph Implementation
Adjacency Matrix
- It is a
(where ) matrix where if a connection exists between node and , then . {cpp}matrix[vi][vj] = 1;if there is a connection,0otherwise.- Clearly, the matrix is going to symmetric if the graph is undirected.
Adjacency List
- It is a collection of
lists, where each list stores the nodes that the -th node is connected to. - Think of it more like a hashmap, where they key is the node, and the value is the list of nodes it is connected to.
Traversal
Breadth-first
- Explore all vertices at current depth before moving on to next depth.
- Visit neighbours of current node, then move on to next node.
Algorithm
- Enqueue the starting node, and mark as visited
- While Queue is not empty
- Dequeue node from start and mark as visited
- Visit all unvisited neighbours of current node, and then enqueue those neighbours
- Repeat above step till queue is empty
Depth-first
- Explore entire depth from vertice before looking at neighbours at same level
Algorithm
- Push onto stack, the starting node, after marking as visited
- While Stack is not empty
- Pop from stack
- Visit all unvisited neighbours of this node and then push to stack
- Continue
Undirected graphs
Connectivity
-
An undirected graph is called "connected" if there exists a path between every pair of nodes in
. -
A subgraph, which is connected, is called a "connected component".
-
Articulation point (Cut Vertex) : The removal of the vertex produces more connected components then there initially was.
- Essentially means that this vertex is a choke point, and it's removal will break graph connectivity, resulting in (possibly) more fully connected graphs that lack connection between two graphs.
-
Cut edge : An edge whose removal produces more connected components then there initially was.
Directed graphs
Terminologies
- Directed multi-graph : There can be multiple directed edges between a node
and . - In-degree (
) : Number of edges for which is terminal - Out-degree (
) : Number of edges for which is initial
Connectivity
- A directed graph is called "strongly connected" if there is a path from
to and from to , whenever they are both present as vertices - A directed graph is called "weakly connected" if the underlying undirected representation of the graph is fully connected.
Transitive closure
In any graph
If a certain node is reachable from another node (maybe through some intermediate nodes), then
Therefore,
- Same vertices as
- An edge
iff there is a path between and in
Transitive closure matrix : An extension to adjacency matrix.
- In adj matrix, if edge exists, then
{cpp}m[i][j] = 1; - in Transitive matrix,
{cpp}T[i][j] = 1;if path exists betweenand
This can be expanded for a digraph as follows
- If there is a directed path between
and in , then in there is a directed edge between and .
Algorithms to construct transitive closure
- Unweighted Graph : Warshalls Algorithm
- Weighted Graph : Floyd-Warshalls Algorithm
Relations
- A connected graph with no cycles obeys
; This is the case of a tree - If a graph has
, then it must contain atleast one cycle - If a connected graph has
then it contains exactly one cycle and is called a unicyclic graph.