- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Applications, Advantages and Disadvantages of Graph

Graphs are used in different disciplines. They are utilised in biology to represent gene interactions, in transportation for route optimisation, and in social networks for user connection analysis. The visual representation of intricate relationships and the capacity to see patterns and trends are two benefits of graphs. However, dealing with large datasets can make graphs bulky and difficult to understand. Additionally, creating graphs can take time and necessitate knowledge. Despite these drawbacks, graphs continue to be an effective tool for data analysis and decision−making across a range of disciplines.

## Methods Used

Set Representation

Linked Representation

Sequential Representaion

## Set Representation

Each vertex in a graph is associated with a set that contains its surrounding vertices in a set representation of the graph. In this method, the edges of the graph are stored either in adjacency sets or hash tables containing sets. Each vertex's set guarantees that there aren't any neighbouring vertices that are duplicates, and it effectively manages sparse graphs. In comparison to alternative representations, it is easier to add and remove edges, and memory utilisation is reduced. When working with networks that have different degrees of connection, this technique is very helpful because it enables effective actions like checking for edges and iterating across nearby vertices.

Adjacency Sets: In the Set Representation of a graph, adjacency sets use sets to record the neighbours of each vertex, preventing duplication and facilitating effective handling of edges.

Hash Tables: Hash tables are used in the context of set representation of graphs to link each vertex with a set containing its neighbouring vertices.

### Algorithm

The graph vertices should be represented by a class or data structure. Each vertex object needs to have a set to contain its neighbouring vertices as well as an ID or label.

Create an empty storage space to hold the graph's vertices (such as an array, vector, or hash table).

For each vertex in the graph:

Create a new vertex object with the specified ID or label for each vertices in the graph.

Add the vertices that are next to it to the adjacency set.

Use the following techniques to add edges between vertices:

Collect the source and destination vertices' vertex objects.

Include the destination vertex in the source vertex's adjacency set.

Implement the following edge removal techniques:

Collect the source and destination vertices' vertex objects.

Eliminate the destination vertex from the source vertex's adjacency set.

Implement additional techniques required for graph operations, such as determining whether an edge exists and obtaining a vertex's neighbours.

### Example

#include <iostream> #include <unordered_map> #include <unordered_set> class Graph { private: std::unordered_map<int, std::unordered_set<int>> adjacencySets; public: void addEdge(int source, int destination) { adjacencySets[source].insert(destination); adjacencySets[destination].insert(source); // If the graph is undirected, add both edges } void removeEdge(int source, int destination) { adjacencySets[source].erase(destination); adjacencySets[destination].erase(source); // If the graph is undirected, remove both edges } void printNeighbors(int vertex) { std::cout << "Neighbors of vertex " << vertex << ": "; for (int neighbor : adjacencySets[vertex]) { std::cout << neighbor << " "; } std::cout << std::endl; } }; int main() { Graph graph; graph.addEdge(1, 2); graph.addEdge(1, 3); graph.addEdge(2, 3); graph.addEdge(3, 4); graph.addEdge(4, 5); graph.printNeighbors(1); graph.printNeighbors(3); graph.removeEdge(2, 3); graph.printNeighbors(1); graph.printNeighbors(3); return 0; }

### Output

Neighbors of vertex 1: 3 2 Neighbors of vertex 3: 4 2 1 Neighbors of vertex 1: 3 2 Neighbors of vertex 3: 4 1

## Linked Representation

Each vertex is represented as a node in a linked list in the graph's Linked Representation. The structure of the graph is formed by these nodes, which connect to one another by pointers or references and hold data about the vertex. Each node also has a linked list or other dynamic data structure that is used to store the edges' neighbouring vertices. This method effectively portrays sparse graphs with various connectedness levels. It supports dynamic graph architectures and permits simple edge addition and removal. However, in comparison to other representations, it could have a slightly larger memory burden. It is advantageous to use Linked Representation when memory flexibility and efficiency are top considerations.

### Algorithm

Locate the graph node that corresponds to vertex 1 in a tree.

Create a new node for vertex1 and add it to the graph if the node cannot be located.

In the graph, locate the node that corresponds to vertex 2.

Create a new node for vertex2 and add it to the graph if the node cannot be located.

To indicate the edge, add vertex2 to the linked list of the node that corresponds to vertex1.

In the undirected graph, link vertex1 to vertex2 in the linked list.

### Example

#include <iostream> #include <unordered_map> #include <list> void AddEdge(std::unordered_map<int, std::list<int>>& graph, int vertex1, int vertex2) { graph[vertex1].push_back(vertex2); graph[vertex2].push_back(vertex1); } int main() { std::unordered_map<int, std::list<int>> graph; AddEdge(graph, 1, 2); AddEdge(graph, 1, 3); AddEdge(graph, 2, 3); for (const auto& entry : graph) { std::cout << "Vertex " << entry.first << " is connected to: "; for (int neighbor : entry.second) { std::cout << neighbor << " "; } std::cout << std::endl; } return 0; }

### Output

Vertex 3 is connected to: 1 2 Vertex 2 is connected to: 1 3 Vertex 1 is connected to: 2 3

## Applications

Graphs are used to simulate user connections on social media platforms, allowing for the investigation of social interactions and the identification of communities.

Graphs are useful in route optimisation, shortest−path calculations, and the design of effective transportation networks.

The topology of networks is represented by graphs, which is helpful for network design, analysis, and troubleshooting.

Graphs simulate metabolic pathways, protein−protein interactions, and gene connections to aid in the study of biological systems.

Graphs are used in recommendation engines to make recommendations for goods, films, or other material based on user preferences and item relationships.

They enable intelligent search and question−answering systems by structuring and connecting information.

Fraud detection, risk assessment, and portfolio optimisation all use graphs.

Graph−based techniques are used for problems including link prediction, classification, and grouping.

Graphs make it easier to understand the links between IoT devices and data flows, which facilitates analysis in IoT applications.

Medical research on drug interactions, patient monitoring, and disease modelling is supported by graphs, which improves healthcare insights.

## Advantages

Graphs offer a simple and understandable visual representation of data, making intricate linkages and relationships easier to comprehend.

Pattern recognition, trend analysis, and anomaly detection are made possible by graphs, which improves the ability to make decisions and solve problems.

For efficient data processing and interpretation, graphs depict a variety of data structures, accurately simulating complicated real−world circumstances.

When working with interconnected data in databases, graph−based topologies make data retrieval and traversal possible.

Graphs are frequently used in social network analysis, to comprehend social interactions, and to pinpoint prominent nodes or users.

Graphs are useful for determining the quickest or most effective routes between places in transportation and logistics.

Graphs drive recommendation engines, which make recommendations for goods, services, or information based on user behaviour and preferences.

Graphs may represent knowledge and information hierarchically, which makes them helpful in applications for artificial intelligence and the semantic web.

In structured data, tasks like clustering, classification, and link prediction are performed using graph−based machine learning techniques.

Finding the greatest match or effectively scheduling work are just two examples of the many challenges that graph algorithms may help with.

## Disadvantages

Graphs can become difficult to manage and complex when working with enormous datasets or when there are a lot of nodes and edges. It may be difficult to adequately analyse and comprehend the data due to this intricacy.

Storing graphs can consume a lot of memory, particularly for dense graphs with a lot of nodes and edges. Memory use can become an issue as the graph gets bigger.

Finding the shortest path through a huge graph, for example, can be a time− and computational−intensive task. Performance problems may result from this, especially in real−time applications.

Graphs can have a variety of structures, with some nodes having noticeably more connections than others. It may be difficult to use common techniques or derive useful inferences from the data due to this non−uniformity.

When working with high−dimensional graphs, it can be challenging to visualise complicated graphs and may not necessarily give a clear representation of the underlying data.

Missing or incorrect data might cause inconsistencies in the graph, which can compromise the analysis's quality and dependability.

## Conclusion

Graphs are flexible and frequently utilised in a variety of disciplines, such as biology, transportation, and social networks. They are useful tools for data analysis because they can visualise complex relationships and can find patterns. However, working with large datasets can get complicated and require more memory. Additionally, creating a graph takes time and requires knowledge. Graphs continue to be a useful tool for problem solving and decision−making despite these shortcomings. Graphs can continue to offer helpful insights and support in a variety of applications across several disciplines by utilising appropriate representations, such as set and linked representations, and putting into practise efficient algorithms.