Graphs Updated
Graphs Updated
Graphs
Define Graphs:
A graph is a visual representation of vertices and edges sharing some connection or relationship.
Adjacent vertices can be called those vertices that are connected to the same edge with each other.
Graphs
Order: Order defines the total number of vertices present in the graph.
Size: Size defines the number of edges present in the graph.
Self-loop: It is the edges that are connected from a vertex to itself.
Isolated vertex: It is the vertex that is not connected to any other vertices in the graph.
Vertex degree: It is defined as the number of edges incident to a vertex in a graph.
Weighted graph: A graph having value or weight of vertices.
Unweighted graph: A graph having no value or weight of vertices.
Directed graph: A graph having a direction indicator.
Undirected graph: A graph where no directions are defined.
Types of Graphs
Two types
Breath First Search Algorithm
Depth First Search Algorithm
Breath First Search Algorithm (Queue –FIFO)
Breath First Search Algorithm (Queue –FIFO)
Algorithm
Start putting anyone vertices from the graph at the back of the queue.
First, move the front queue item and add it to the list of the visited node.
Next, create nodes of the adjacent vertex of that list and add them which have not been visited
yet.
Keep repeating steps two and three until the queue is found to be empty.
Complexity: 0(V+E) where V is vertices and E is edges.
Applications
It is used to determine the shortest path and minimum spanning tree.
It is also used in web crawlers to creates web page indexes.
It is also used as powering search engines on social media networks and helps to find out peer-
to-peer networks in Bit-Torrent.
BFS Sample code
Set all nodes to "not visited";
q = new Queue();
q.enqueue(initial node);
while ( q ? empty ) do
{
x = q.dequeue();
if ( x has not been visited )
{
visited[x] = true; // Visit node x !
The topologically sorted graph ensures to sort vertex that comes in the pathway.
Strongly Connected Components
A strongly connected component is the component of a directed graph that has a path
from every vertex to every other vertex in that component. It can only be used in a directed
graph.
For example, The below graph has two strongly connected components {1,2,3,4} and
{5,6,7}
since there is path from each vertex to every other vertex in the same strongly connected
component.
Strongly Connected Components
Now, let’s start a dfs call from vertex 1 to visit other vertices
Spanning Tree
The number of spanning trees that can be made from the above complete graph equals to
nn-2 = 44-2 = 16.
Therefore, 16 spanning trees can be created from the above graph.
The maximum number of edges that can be removed to construct a spanning tree equals to
e-n+1 = 6 - 4 + 1 = 3.