0% found this document useful (0 votes)
8 views

Graphs Updated

Uploaded by

DR.MIYALVAGANAN
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Graphs Updated

Uploaded by

DR.MIYALVAGANAN
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

GRAPHS

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 !

for ( every edge (x, y) /* we are using all edges ! */ )


if ( y has not been visited )
q.enqueue(y); // Use the edge (x,y) !!!
}
}
Depth First Search Algorithm (Stack –LIFO)
Depth First Search Algorithm (Stack –LIFO)
 Algorithm
 Start by putting one of the vertexes of the graph on the stack's top.
 Put the top item of the stack and add it to the visited vertex list.
 Create a list of all the adjacent nodes of the vertex and then add those nodes to the unvisited at the
top of the stack.
 Keep repeating steps 2 and 3, and the stack becomes empty.
 Applications
 DFS finds its application when it comes to finding paths between two vertices and detecting
cycles.
 Also, topological sorting can be done using the DFS algorithm easily. DFS is also used for one-
solution puzzles.
DFS Sample Code
DFS(G,v) ( v is the vertex where the search starts )
Stack S := {}; ( start with an empty stack )
for each vertex u, set visited[u] := false;
push S, v;
while (S is not empty) do
u := pop S;
if (not visited[u]) then
visited[u] := true;
for each unvisited neighbour w of uu
push S, w;
end if
end while
END DFS()
Topological sorting
 Topological sorting of a graph follows the algorithm of ordering the vertices linearly so that each
directed graph having vertex ordering ensures that the vertex comes before it.

 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 above graph can be represented as G(V, E)


 The spanning tree of the above graph would be represented as G`(V`, E`).
 In this case, V` = V means that the number of vertices in the spanning tree would be the
same as the number of vertices in the graph, but the number of edges would be different.
 Therefore, the number of edges can be written as: E` € E
 It can also be written as: E` = |V| - 1
Spanning Tree
 Two conditions exist in the spanning tree, which is as follows:
 The number of vertices in the spanning tree would be the same as the number of vertices in the original graph.
V` = V
 The number of edges in the spanning tree would be equal to the number of edges minus 1.
E` = |V| - 1
 The spanning tree should not contain any cycle.
 The spanning tree should not be disconnected.
 The above graph contains 5 vertices. As we know, the vertices in the spanning tree would be the same
as the graph; therefore, V` is equal 5. The number of edges in the spanning tree would be equal to (5 -
1), i.e., 4. The following are the possible spanning trees:
Minimum Spanning Tree
 The minimum spanning tree is a spanning tree whose sum of the edges is minimum.
 The following are the spanning trees that we can make from the above graph.
 The first spanning tree is a tree in which we have removed the edge between the vertices 1 and 5
shown as below:
The sum of the edges of the above tree is (1 + 4 + 5 + 2): 12
 The second spanning tree is a tree in which we have removed the edge between the vertices 1 and
2 shown as below:
The sum of the edges of the above tree is (3 + 2 + 5 + 4) : 14
 The third spanning tree is a tree in which we have removed the edge between the vertices 2 and 3
shown as below:
The sum of the edges of the above tree is (1 + 3 + 2 + 5) : 11
 The fourth spanning tree is a tree in which we have removed the edge between the vertices 3 and
4 shown as below:
The sum of the edges of the above tree is (1 + 3 + 2 + 4) : 10. The edge cost 10 is minimum so it
is a minimum spanning tree.
Minimum Spanning Tree
 General properties of minimum spanning tree:
 If we remove any edge from the spanning tree, then it becomes disconnected. Therefore, we
cannot remove any edge from the spanning tree.
 If we add an edge to the spanning tree then it creates a loop. Therefore, we cannot add any edge to
the spanning tree.
 In a graph, each edge has a distinct weight, then there exists only a single and unique minimum
spanning tree. If the edge weight is not distinct, then there can be more than one minimum
spanning tree.
 A complete undirected graph can have an n n-2 number of spanning trees.
 Every connected and undirected graph contains atleast one spanning tree.
 The disconnected graph does not have any spanning tree.
 In a complete graph, we can remove maximum (e-n+1) edges to construct a spanning tree.
Minimum Spanning Tree
 Let's understand the last property through an example.
 Consider the complete graph which is given below:

 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.

You might also like