0% found this document useful (0 votes)
12 views14 pages

Topic 1 - Graphs

A graph is a non-linear data structure made up of vertices (nodes) and edges that connect them, denoted as G(V, E). There are various types of graphs, including directed, undirected, cyclic, and weighted graphs, each with specific characteristics and applications in areas such as social networks, web page ranking, and transportation. Graphs offer flexibility in representing relationships and solving complex problems but can also present challenges in complexity, computational costs, and visualization.

Uploaded by

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

Topic 1 - Graphs

A graph is a non-linear data structure made up of vertices (nodes) and edges that connect them, denoted as G(V, E). There are various types of graphs, including directed, undirected, cyclic, and weighted graphs, each with specific characteristics and applications in areas such as social networks, web page ranking, and transportation. Graphs offer flexibility in representing relationships and solving complex problems but can also present challenges in complexity, computational costs, and visualization.

Uploaded by

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

Graphs

What is a Graph?

●​ A graph is a non-linear data structure consisting of vertices and edges.


●​ The vertices are sometimes also referred to as nodes and the edges are
lines or arcs that connect any two nodes in the graph.
●​ More formally a Graph is composed of a set of vertices( V ) and a set of
edges( E ). The graph is denoted by G(V, E).

Graph Terminologies

●​ Vertices: Vertices are the fundamental units of the graph. Sometimes,


vertices are also known as vertex or nodes. Every node/vertex can be
labeled or unlabelled.
●​ Edges: Edges are drawn or used to connect two nodes of the graph. It
can be ordered pair of nodes in a directed graph. Edges can connect
any two nodes in any possible way. There are no rules. Sometimes,
edges are also known as arcs. Every edge can be labelled/unlabelled.
●​ Path: A path can be defined as the sequence of nodes that are followed
in order to reach some terminal node V from the initial node U.
●​ Closed Path: A path will be called as closed path if the initial node is
same as terminal node. A path will be closed path if V0=VN.
●​ Cycle: A cycle can be defined as the path which has no repeated
edges or vertices except the first and last vertices.
Types of graphs

1.​ Null Graph: A graph is known as a null


graph if there are no edges in the graph.

2.​ Trivial Graph: A graph having only a single


vertex, it is also the smallest graph possible.

3.​ Undirected graph: A graph in which


the edges are not associated with the
directions or are not attached to any of the
directions is known as an undirected graph.
In the diagram, since an edge exists
between vertex A and B, the vertices can
be traversed from B to A and from A to B.
4.​ Directed graph
In a directed graph, edges form an
ordered pair.
Edges represent a specific path
from some vertex A to another
vertex B.
Node A is called the initial node
while node B is called the terminal
node.

5.​ Connected Graph: The graph in which


from one node we can visit any other node in
the graph is known as a connected graph.

6.​ Disconnected Graph: The graph in which


at least one node is not reachable from a node is
known as a disconnected graph.
7.​ Cyclic Graph: A graph containing at least one
cycle is known as a Cyclic graph.

8.​ Bipartite Graph: A graph in which the vertex


can be divided into two sets such that the vertex in
each set does not contain any edge between them.

9.​ Weighted Graph: A graph in which the


edges are already specified with suitable
weight is known as a weighted graph.
Weighted graphs can be further classified as
directed weighted graphs and undirected
weighted graphs.
Advantages

1.​ Flexibility in Representing Relationships: Graph Data Structure used to


represent a wide range of relationships as we do not have any restrictions
like previous data structures (Tree cannot have loops and have to be
hierarchical. Arrays, Linked List, etc are linear)
2.​ Versatility in Problem-Solving: They can be used to model and solve a
wide range of problems, including pathfinding, data clustering, network
analysis, and machine learning.
3.​ Real-World Modeling: Any real world problem where we certain set of
items and relations between them can be easily modeled as a graph and a
lot of standard graph algorithms like BFS, DFS, Spanning Tree, Shortest
Path, Topological Sorting and Strongly Connected
4.​ Simplified Representation of Complex Data: Graph Data Structure can
be used to represent complex data structures in a simple and intuitive way,
making them easier to understand and analyze.

Limitations

1.​ Complexity and Accessibility: Graph Data Structure can be complex and
difficult to understand, especially for people who are not familiar with graph
theory or related algorithms.
2.​ High Computational Costs: Creating and manipulating graphs can be
computationally expensive, especially for very large or complex graphs.
3.​ Challenges in Algorithm Design: Graph algorithms can be difficult to
design and implement correctly, and can be prone to bugs and errors.
4.​ Visualization and Analysis Challenges: Graph Data Structure can be
difficult to visualize and analyze, especially for very large or complex
graphs, which can make it challenging to extract meaningful insights from
the data.

Applications

1.​ Social Networks:


●​ Social networks like Facebook, Twitter, and LinkedIn use graphs to
represent users as nodes and their relationships (e.g., friendships,
follows, or connections) as edges.
●​ Examples- Finding influencers, community detection, and
recommendation systems (e.g., "People You May Know").

2.​ Web Page Ranking


●​ The World Wide Web is a massive directed graph where web pages
are nodes, and hyperlinks between them are directed edges.
●​ Examples: Algorithms like Google's PageRank use graphs to rank
web pages based on their importance.

3.​ Transportation and Navigation


●​ Road networks, flight routes, and railways are represented as
graphs, where intersections or stops are nodes, and roads or routes
are edges.
●​ Examples: Shortest path algorithms (e.g., Dijkstra’s or A* algorithm)
are used for GPS navigation, airline scheduling, and delivery route
optimization.

4.​ Network Analysis


●​ Graphs are used to model communication networks, power grids,
and computer networks. Nodes represent devices (e.g., routers,
computers), and edges represent connections.
●​ Examples: Optimizing data flow, detecting network bottlenecks, and
designing efficient network topologies.

5.​ Biological Networks


●​ In biology, graphs are used to represent protein-protein interaction
networks, metabolic pathways, and gene regulatory networks.
●​ Examples: Understanding disease mechanisms, drug discovery,
and modeling ecosystems.

6.​ Recommendation Systems


●​ Products, users, and their interactions (e.g., purchases, ratings) are
represented as graphs.
●​ Examples: Personalized recommendations on platforms like
Amazon, Netflix, and Spotify using collaborative filtering techniques.

7.​ Database Query Optimization


●​ Databases can be represented as graphs, where tables are nodes,
and relationships between tables (e.g., joins) are edges.
●​ Examples: Optimizing query execution plans by finding the shortest
or most efficient paths between tables.

8.​ Game Development


●​ In video games, maps, levels, or AI decision-making can be
represented as graphs. Nodes represent game elements (e.g.,
locations or states), and edges represent possible actions or
transitions.
●​ Examples: Pathfinding algorithms like A* for non-player characters
(NPCs) to navigate game environments.
Graph traversals shortest path algorithm
Graph algorithms are methods used to manipulate and analyze graphs, solving
various range of problems like finding the shortest path, cycles detection.

Common algorithms used for graph traversals shortest path are:

1.​ BFS (Breadth First Search)


2.​ DFS (Depth First Search)

Breadth First Search


BFS is a graph traversal approach in which you start at a source node and layer
by layer through the graph, analyzing the nodes directly related to the source
node.
Popular graph algorithms like Dijkstra’s shortest path, Kahn’s Algorithm, and
Prim’s algorithm are based on BFS.

Algorithm
Depth First Search
The algorithm starts from a given source and explores all reachable vertices from
the given source. It is similar to Preorder Tree Traversal where we visit the root,
then recur for its children.

Algorithm

You might also like