Design Analysis Algorithm 4
Design Analysis Algorithm 4
A graph data structure is a collection of nodes that have data and are connected to other
nodes. Every relationship is an edge from one node to another.
More precisely, a graph is a data structure (V, E) that consists of
• A collection of vertices V
• A collection of edges E, represented as ordered pairs of vertices (u,v)
V = {0, 1, 2, 3}
E = {(0,1), (0,2), (0,3), (1,2)}
G = {V, E}
Graph Terminology
• Adjacency: A vertex is said to be adjacent to another vertex if there is an edge
connecting them. Vertices 2 and 3 are not adjacent because there is no edge between
them.
• Path: A sequence of edges that allows you to go from vertex A to vertex B is called a
path. 0-1, 1-2 and 0-2 are paths from vertex 0 to vertex 2.
Directed Graphs
A directed graph is a set of vertices (nodes) connected by edges, with each node having a
direction associated with it.
Edges are usually represented by arrows pointing in the direction the graph can be traversed.
Undirected Graph
In an undirected graph the edges are bidirectional, with no direction associated with them.
Hence, the graph can be traversed in either direction. The absence of an arrow tells us that the
graph is undirected.
Connected Graph
A connected graph is a graph in which there is always a path from a vertex to any other vertex.
Regular Graph
A graph in which degree of all the vertices is same is called as a regular graph.
If all the vertices in a graph are of degree ‘k’, then it is called as a “k-regular graph“.
Examples
In these graphs,
• All the vertices have degree-2.
• Therefore, they are 2-Regular graphs.
Complete Graph
A graph in which exactly one edge is present between every pair of vertices is called as a
complete graph.
A complete graph of ‘n’ vertices is represented as Kn.
Examples
Cycle Graph
A simple graph of ‘n’ vertices (n>=3) and n edges forming a cycle of length ‘n’ is called as a cycle
graph.
In a cycle graph, all the vertices are of degree 2.
Examples
In these graphs,
• Each vertex is having degree 2.
Spanning Tree
A spanning tree is a sub-graph of an undirected connected graph G (V, E) if
• It should contain all vertices of G.
• It should contain (|v| -1) edges.
The total number of spanning trees with n vertices that can be created from a complete graph
is equal to n(n-2) .
If we have n = 4, the maximum number of possible spanning trees is equal to 44-2 = 16. Thus,
16 spanning trees can be formed from a complete graph with 4 vertices.