De Santores, Jay Marc A.
Terminology and Representations of Graps
This post discuss the basic definitions in terminologies associated with graphs
and covers adjacency list and adjacency matrix representations of the graph data
structures.
What is a Graph – is an ordered pair G = (V, E) comprising a set of vertices or
nodes and a collection of pairs of vertices from V called edges of the graph.
For example for above graph,
V = {1, 2, 3, 4, 5, 6}
E = {(1. 4), (1, 6), (2, 6), (4, 6), (5, 6) }
Types of Graph:
1. Undirected graph
An undirected graph (graph) is a graph in which edges have no orientation. The edge
(x, y) is identical to the edge (y, x), i,e. they are not ordered pairs. The maximum
number of edges possible in an undirected graph without a loop is n(n -1 )/2.
2. Directed graph
A Directed graph (Di- graph) is a graph in which edges have orientations. i.e. The edge
(x, y)is not identical to the edges (y, x).
De Santores, Jay Marc A.
3. Directed Acyclic Graph (DAG)
A Directed Acyclic Graph (DAG) is a directed graph that contains no cycles.
4. Mutigraph
is an undirected graph in which multiple edges (and sometimes loops) are
allowed. Multiple edges are two or more edges that connect the same two vertices. A
loop is an edge (directed or undirected) that connects a vertex to itself; it may be
permitted or not, according to the application.
5. Simple Graph
A simple graph, as opposed to a multigraph, is an undirected graph in which both
multiple edges and loops are disallowed. In a simple graph with
6. Weighted and Unweighted Graph
A unweighted graph do not have any a value (weight) associated with every edge in the
graph. In other words, weighted graph is a weighted graph with all edge weight as 1.
Unless specified otherwise, all graphs are assumed to be unweighted by default.
De Santores, Jay Marc A.
7. Complete graph
A complete graph is one in which every two vertices are adjacent: all edges that could
exist are present.
8. Connected Graph
A connected graph has path between every pair of vertices. In other words, there are no
unreachable vertices. A disconnected graph is a graph which is not connected.
De Santores, Jay Marc A.
Undirected graph definition
An undirected graph is graph, i.e., a set of objects (called vertices or nodes) that are
connected together, where all the edges are bidirectional. An undirected graph is
sometimes called an undirected network. In contrast, a graph where the edges point in
a direction is called a directed graph.
When drawing an undirected graph, the edges are typically drawn as lines between
pairs of nodes, as illustrated in the following figure.
One can formally define an undirected graph as $G= (\mathcal{N},\mathcal{E})$,
consisting of the set $\mathcal{N}$ of nodes and the set $\mathcal{E}$ of edges, which
are unordered pairs of elements of $\mathcal{N}$.
De Santores, Jay Marc A.
Directed graph definition
A directed graph is graph, i.e., a set of objects (called vertices or nodes) that are
connected together, where all the edges are directed from one vertex to another. A
directed graph is sometimes called a digraph or a directed network. In contrast, a graph
where the edges are bidirectional is called an undirected graph.
When drawing a directed graph, the edges are typically drawn as arrows indicating the
direction, as illustrated in the following figure.
One can formally define a directed graph as G=(N,E)G=(N,E), consisting of the
set NN of nodes and the set EE of edges, which are ordered pairs of elements of NN.
De Santores, Jay Marc A.
Graph Traversals
In computer science, graph traversal (also known as graph search) refers to the
process of visiting (checking and/or updating) each vertex in a graph. Such traversals
are classified by the order in which the vertices are visited. Tree traversal is a special
case of graph traversal.
Breadth First Search
Graph traversals
Graph traversal means visiting every vertex and edge exactly once in a well-defined
order. While using certain graph algorithms, you must ensure that each vertex of the
graph is visited exactly once. The order in which the vertices are visited are important
and may depend upon the algorithm or question that you are solving.
During a traversal, it is important that you track which vertices have been visited. The
most common way of tracking vertices is to mark them.
Breadth First Search (BFS)
There are many ways to traverse graphs. BFS is the most commonly used approach.
BFS is a traversing algorithm where you should start traversing from a selected node
(source or starting node) and traverse the graph layerwise thus exploring the neighbour
nodes (nodes which are directly connected to source node). You must then move
towards the next-level neighbour nodes.
As the name BFS suggests, you are required to traverse the graph breadthwise as
follows:
1. First move horizontally and visit all the nodes of the current layer
2. Move to the next layer
De Santores, Jay Marc A.
Traversing child nodes
A graph can contain cycles, which may bring you to the same node again while
traversing the graph. To avoid processing of same node again, use a boolean array
which marks the node after it is processed. While visiting the nodes in the layer of a
graph, store them in a manner such that you can traverse the corresponding child
nodes in a similar order.
In the earlier diagram, start traversing from 0 and visit its child nodes 1, 2, and 3. Store
them in the order in which they are visited. This will allow you to visit the child nodes of
1 first (i.e. 4 and 5), then of 2 (i.e. 6 and 7), and then of 3 (i.e. 7) etc.
To make this process easy, use a queue to store the node and mark it as 'visited' until
all its neighbours (vertices that are directly connected to it) are marked. The queue
follows the First In First Out (FIFO) queuing method, and therefore, the neigbors of the
node will be visited in the order in which they were inserted in the node i.e. the node
that was inserted first will be visited first, and so on.