Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   
   HTML   
   CSS   
   JAVA SCRIPT   
   JQUERY   




GRAPH


A Graph is composed of a set of 'Vertices' and a set of 'Edges'.


Let us understand 'Vertices' and 'Edges' with the below example :


java_Collections

In the above diagram, a, b, c, d and e are the 'vertices'. And the lines connecting them are the 'edges'.


Just consider, you are a bus driver. And a,b,c,d and e are destinations and the lines connecting them are the roads.


How to represent an edge?


An 'edge' is a pair of vertices.


So, the 'edge' that is connecting vertices 'a' and 'b' can be represented as,


e = (a,b)

Similarly,


The 'edge' connecting 'c' and 'e' is represented as,


e = (c,e)

How many edges are there in the above diagram?


E = {(a,b),(a,c),(a,d),(b,c),(c,d),(c,e),(d,e)}

Note :The edge (a,b) is same as (b,a).

How many vertices are there in the above diagram?


V = {a, b, c, d, e}

How many types of Graphs are there ?


There are two types of Graphs :


  1. Undirected Graph

  2. Directed Graph

The Graph we have seen above is an undirected graph. Where no direction is specified.


And in a directed graph, direction would be specified.


If we modify the above graph to a directed graph :


java_Collections

In the above graph, the direction is specified.


Where a,b is an edge, but b,a cannot be represented as an edge. Guess why? As the direction is specified.


Graph Terminology


Let us take the undirected graph to explain the graph terminologies.


java_Collections

Adjacent Vertices


Vertices connected by an edge are Adjacent Vertices.


Example :


(a,e) is not an adjacent vertices but (a,c) can be called adjacent vertices, as it is connected by an edge.


Degree of Vertex


A degree of vertex is the number of adjacent vertices it has.


Example :


The degree of vertex 'a' is 3 as it has 3 adjacent vertices i.e. (a,b) (a,c) and (a,d).


Path :


Let us take the above example to explain this.


You are a bus driver and a, b, c, d, e are bus stops. And the edges are the roads.


Now, if you are asked to travel from a to c. How would you do it?


You could have taken the path a to c or ( a,c).


Or to drop passengers to location b and c. You might have taken the path ( a,b,e,c). And so on.


And the same logic applies here. ( a,c), ( a,b,e,c) is a path but ( a,b,c) is not a path as there is no edge connecting b and c.


Connected Graph


If there is a path between two vertices, they are said to be connected.


Connected Graph


java_Collections

Note :Just remember d and e are connected, as there is a path between them.

The Graph that is not connected


java_Collections

There is no path from a, c, d and b,e.


Tree


A Tree in a Graph is connected graph without cycles.


Let us understand with an example :


The below graph is not a Tree. Guess why ?


java_Collections

The below graph is not a Tree because it is forming a cycle. i.e. a,c,d,a.


Now, if we remove the edge (a,d).


java_Collections

It becomes are tree as it is not forming a cycle anymore.


Forest


Forest is a collection of Trees.


Complete Graph


It is a Graph in which there is a edge between every pair of vertices.


java_Collections

The above graph is a Complete Graph as there is not a single edge that is missing. In other words, you cannot draw anymore edges connecting two vertices.


How many edges does a complete graph have?


In the above case we have 5 Vertices.


Let us suppose, 'n' be the number of vertices.


Then we can write,


Number of edges in a complete graph = n(n-1)/2


Now, if we consider the above example and substitute the value n with number of vertices,


Number of edges in a complete graph = 5(5-1)/2

                   = 10


Now, to cross verify, if we count the number of edges in the above graph. We can see it's 10.


Spanning Tree


Spanning tree as the name suggests is a tree that is derived from an existing graph.


Example :


Let us say, in the below graph, the vertices are the number of cities. And the edges are the roads connecting them.


Now, let's say, we want to cut of the extra roads connecting the cities. i.e. If we want to reach city 'c'. We can take the path (a,c) or (a,d,c) or (a,d,e,c) and so on.


We don't want that. We just need a single road that goes from a to c.


And thus comes spanning tree.


Let us take the below graph,


java_Collections

If we want to make a spanning tree out of the above graph. We need to convert it to a tree and make sure that all the vertices are present.


java_Collections

Fair enough !


Now, it is a spanning tree derived from the above graph.