Unit 4 Graph
Unit 4 Graph
Unit 4 Graph
Graph
Introduction:-
A graph data structure is a way to represent a collection of interconnected points, called nodes or vertices,
where some of these points are linked by lines or edges. It's a visual way to show how different elements
are related to one another. Graphs are used to model and solve various problems involving relationships
and connections in fields like computer science, mathematics, and real-world applications.
This question was given to a famous mathematician called Leonhard Euler. To understand it better at first
simply it.
Question: Can you draw each line p, q, r, s, t, u and v only once, without removing your pencil from the
paper (you may start at any point)?
Answer: NO
Try another:
Some Important Terms:
A route around a graph that visits every vertex once is called a simple path.
A route around a graph that visits every edge once is called an Euler path.
Vertices A, B and D have degree 3 and vertex C has degree 5, so this graph has four
vertices of odd degree. So it does not have an Euler Path.
Classification of Graph
Simple Graph
A simple Graph is one in which there is only one edge connecting a pair of vertices. (as shown in
the image above). A railway track connecting two different cities is a perfect example of a
simple Graph.
Multi Graph
You search for a location in Google maps and it shows multiple paths taking you to the same
location. That's a multi Graph in which more than one edge could be connecting two vertices.
Complete Graph
In a complete graph, each vertex is adjacent to all of its vertices i.e. all the vertices in the graph
are connected to each other.
Direct Graph
A direct graph has edges that connect to an ordered pair of vertices. If an edge is connecting a
pair of vertices (1 and 2) from 1 to 2, the edge will have an arrow directed to 2. If we change the
direction of the arrow, the meaning of the graph will change.
They are also known as Digraphs and are useful in marking hierarchies in social networks and
roadways.
Undirected Graph:
In an undirected graph, edges have no direction. If there is an edge between nodes A and B, you
can travel from A to B or from B to A. It represents symmetric relationships.
Representation of Graph
Let's say that we have to store a graph in the computer's memory. What technique will we use?
Adjacency matrix
An adjacency matrix is used to represent the mapping between vertices and edges of the graph. It
can represent the undirected graph, directed graph, weighted undirected, and weighted directed
graph. How?
If there are n vertices in an undirected graph, the adjacency matrix for that graph will be n*n.
And each matrix will be denoted by either O or 1 depending upon the existence of the edge
between two nodes.
So, any matrix adj[a][b] will denote the edges between vertex a and vertex b.
This matrix will be 1 if there exists an edge between the two nodes, else it will be 0.
Xab = 0 (if not a single path exists between the two vertices)
You'll find that the cross between a and a, or b and b says 0, that means there's no self loop.
Also, the cross between A*B and B*A says 1, which means it's an undirected graph
Adjacency list
Adjacency list uses linked lists to represent a graph. For each node in a graph, there exists a
linked list that stores the node value and a pointer to the next adjacent node.
In the image, we can see that there is a linked list for every node in the graph. Vertex a is
connected to both vertex b , and vertex d, as you can see in the adjacency list representing the
graph.
Graph Traversal
Some application of the graph required systematically visiting all the nodes. In a graph, we can
reach to the vertex from multiple paths existing in the graph. There are two standard traversal
methods generally used with graph:
Breadth-First Search
This search operation traverses all the nodes at a single level/hierarchy before moving on to the
next level of nodes. BFS algorithm starts at the first node (in this picture - 1) and traverses all the
nodes adjacent to 1 i.e. 2 and 3. After all the nodes at the first level are traversed, it proceeds to
the next level repeating the same process.
For example - After vertices 2 and 3 are traversed, the BFS algorithm will then move to the first
adjacent node of 2 i.e. 4, and traverse all its adjacent nodes. This goes on until all the nodes in
the graph are searched.
Depth-First Search
This algorithm traverses the nodes in a vertical fashion. It starts with the root node and traverses
one adjacent node and all its child nodes before moving on to the next adjacent node.
Example - The algorithm will start with 1 and search all the nodes connected to 2 - 4 and 5, and
then move to the next adjacent node to 1 i.e. 3. The order would be given as: 1 -> 2 -> 4 -> 5 ->
3.
Minimum Spanning Tree
A Minimum Spanning Tree (MST) is a fundamental concept in graph theory and network design.
It is a subset of the edges of an undirected, connected graph that connects all the nodes while
minimizing the total edge weight. In simpler terms, an MST is a tree-like subgraph that includes
all the nodes of the original graph with the minimum possible total edge weight.
Here's a basic idea of how you can imagine an MST: Imagine a set of cities with roads
connecting them, and each road has a distance (or cost) associated with it. The Minimum
Spanning Tree would be the subset of roads that connect all the cities with the shortest total
distance (or cost), ensuring that all cities are reachable.
MSTs are essential in optimizing network design and connectivity while minimizing resource
usage, and they have many practical applications in computer science, operations research, and
various engineering and logistical fields.
Applications of Graph
Real-world applications
If you look closer, the whole wide universe could be a graph in itself. We can think of the universe at the
very start as a collection of abstract relations between abstract elements. Some researchers do have
explanations about this theory, and it only seems logical. (I’m attaching one of the researches here)
But let’s not go that deep for now, and look at some real-world applications of graph data structure that
we can actually see and experience.
Social networks - This is one of the most basic applications of a graph. A social graph illustrates
connections among people and organizations in a social network. Example: Facebook
Biological networks - Remember when we told you that the whole universe can be a graph? We
were not lying. Our environment is also a huge graph that includes multiple hierarchies, brain
networks, food chains, etc.
Web graph - World Wide Web is another huge graph data set where all the documents are
connected to each other using hyperlinks. In this case, these hyperlinks can be considered edges.
Road networks/navigation - In the computer science world, all navigation problems are
fittingly termed graph problems. In a huge city with hundreds of roads and alleys, graphs guide
you from point A to point B.
You would have seen how the roads are connected in the form of vertices and edges on many
modern apps like Google Maps, Uber, Maze, etc.
Not only that, but it also helps you find the shortest possible route.
Product recommendation - How do these e-commerce websites often know what you want, and
recommend specific products?
Simple. They use the graph data structure to establish connections between what past users like
you have bought.
Block Chains - One thing you must have heard of in recent years is block Chain technology. It is
being deemed to be the future of all transactions.
In a distributed ledger, the blocks act as vertices and they’re all connected through edges. That’s
how each user has the access to the required data and all transactions are transparent.
In Computer Science
Graphs define the flow of computation. A control flow graph, originally developed by Frances E. Allen,
acts as the graphical representation of computation during the execution of programs