Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
82 views

Module 9 - Graphs

1. The document defines key terms related to graphs including vertices, edges, weighted graphs, directed graphs, paths, cycles, and degrees of vertices. 2. It discusses Dijkstra's algorithm for finding the shortest path between two nodes in a graph. 3. The document describes different types of graphs like undirected graphs, directed graphs, complete graphs, cyclic graphs, acyclic graphs, and weighted graphs. It also discusses graph representations using adjacency matrices and adjacency lists.

Uploaded by

Loki Legends
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
82 views

Module 9 - Graphs

1. The document defines key terms related to graphs including vertices, edges, weighted graphs, directed graphs, paths, cycles, and degrees of vertices. 2. It discusses Dijkstra's algorithm for finding the shortest path between two nodes in a graph. 3. The document describes different types of graphs like undirected graphs, directed graphs, complete graphs, cyclic graphs, acyclic graphs, and weighted graphs. It also discusses graph representations using adjacency matrices and adjacency lists.

Uploaded by

Loki Legends
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

IT07-IT07L Data Structure and Algorithm

1
Module 9: Graphs

Module 009: Graphs

Module Learning Outcomes:


1. Define terms related to graphs including: vertices, edges, weighted graph,
directed graph, undirected graph, adjacent vertices, path, length of a path,
cycle, simple cycle, connected vertices, connected components, adjacency
set, degree of a vertex, predecessors, successors.
2. Define shortest path and discuss, with diagrams, Dijkstra's algorithm for
finding the shortest path from node x to node y of a graph.

Introduction to Graphs
A graph is a non-linear kind of data structure made up of nodes or vertices and edges. The
edges connect any two nodes in the graph, and the nodes are also known as vertices.

A map is a well-known example of a graph. In a map various connections are made


between the cities. The cities are connected via roads, railway lines and aerial network. We
can assume that the graph is the interconnection of cities by roads.

Königsberg bridge problem, a recreational mathematical puzzle, set in the old Prussian city
of Königsberg (now Kaliningrad, Russia), that led to the development of the branches of
mathematics known as topology and graph theory. In the early 18th century, the citizens
of Königsberg spent their days walking on the intricate arrangement of bridges across the
waters of the Pregel (Pregolya) River, which surrounded two central landmasses connected
by 7 bridges. According to folklore, the question arose of whether a citizen could take a
walk through the town in such a way that each bridge would be crossed exactly once.

In 1735 the Swiss mathematician Leonhard Euler used graph theory to solve Seven
Bridges of Königsberg problem. “Is there a possible way to traverse every bridge exactly
once?” – Euler

A B
D
Leonhard Euler presented a solution to this problem, concluding that such a walk was
impossible.

Course Module
It would be nearly 150 years before mathematicians would picture the Königsberg bridge
problem as a graph consisting of nodes (vertices) representing the landmasses and arcs
(edges) representing the bridges. The degree of a vertex of a graph specifies the number of
edges incident to it.

Defining the degree of a vertex to be the number of edges incident to it, Euler showed that
there is a walk starting at any vertex, going through each edge exactly once and terminating
at the start vertex if the degree of each, vertex is even. A walk which does this is called
Eulerian Walk. (Conditions: The Degree is Even; Zero Odd Vertex or 2 Odd Vertex). There is
no Eulerian walk for the Koenigsberg bridge problem as all four vertices are of odd degree.

A graph contains a set of points known as nodes (or vertices) and set of links known as
edges (or Arcs) which connects the vertices. A graph G is represented as G = ( V , E ), where
V is set of vertices and E is set of edges.

Example:
Graph G can be defined as G = ( V , E ). Where V = {A,B,C,D,E} and
E = {(A,B),(A,C)(A,D),(B,D),(B,E),(C,D),(E,D)}. This is a graph with 5 vertices and 7
edges.

IT07-IT07L Data Structure and Algorithm


3
Module 9: Graphs

Graph Terminology
• Vertex: An individual data element of a graph is called as Vertex. Vertex is also known as
node. In above example graph, A, B, C, D & E are known as vertices.
• Edge: An edge is a connecting link between two vertices. Edge is also known as Arc. An edge
is represented as (starting Vertex, ending Vertex). In above graph, the link between vertices
A and B is represented as (A,B).

Types of Graphs
• Undirected Graph: An undirected graph comprises a set of nodes and links connecting them.
The order of the two connected vertices is irrelevant and has no direction. You can form an
undirected graph with a finite number of vertices and edges.

• Directed Graph: A directed graph also referred to as a digraph, is a set of nodes connected by
edges, each with a direction.

• Complete Graph: If a graph G= (V, E) is also a simple graph, it is complete. Using the edges,
with n number of vertices must be connected. It's also known as a full graph because each
vertex's degree must be n-1.

Course Module
• Cyclic or Cycle Graph: A graph having cycle is called cycle graph. In this case the first and last
nodes are the same. A closed simple path is a cycle.

• Acyclic Graph: When there are no cycles in a graph, it is called an acyclic graph.

• Weighted Graph: A graph G= (V, E) is called a labeled or weighted graph because each edge
has a value or weight representing the cost of traversing that edge.

• Simple Graph: If each pair of nodes or vertices in a graph G=(V, E) has only one edge, it is a
simple graph. As a result, there is just one edge linking two vertices, depicting one-to-one
interactions between two elements.

IT07-IT07L Data Structure and Algorithm


5
Module 9: Graphs

• Pseudo Graph: If a graph G= (V, E) contains a self-loop besides other edges, it is a


pseudograph.

Other terminologies of Graphs in Data Structure


• Outgoing Edge: A directed edge is said to be outgoing edge on its origin vertex. • Incoming
Edge: A directed edge is said to be incoming edge on its destination vertex. • Degree: Total
number of edges connected to a vertex is said to be degree of that vertex. • Indegree: Total
number of incoming edges connected to a vertex is said to be indegree of that vertex.
• Outdegree: Total number of outgoing edges connected to a vertex is said to be outdegree of
that vertex.
• Parallel edges or Multiple edges: If there are two undirected edges to have the same end
vertices, and for two directed edges to have the same origin and the same destination. Such
edges are called parallel edges or multiple edges.
• Self-loop: An edge (undirected or directed) is a self-loop if its two endpoints coincide. •
Simple Graph: A graph is said to be simple if there are no parallel and self-loop edges. •
Adjacent nodes: When there is an edge from one node to another then these nodes are called
adjacent nodes.
• Walk: Defined as a finite alternating sequence of vertices and edges, beginning and ending
with vertices, such that each edge is incident with the vertices preceding and following it. (i.e., if
we traverse a graph then we get a walk.) Here, 1->2->3->4->2->1->3 is a walk.
Course Module
• Closed walk: A walk which is to begin and end at the same vertex is called close walk.
Otherwise, it is an open walk.
In the above diagram:
1->2->3->4->5->3 is an open walk.
1->2->3->4->5->3->1 is a closed walk.
• Path: A open walk in which no vertex appears more than once is called a path. Here 6->8-
>3->1->2->4 is a Path

• Length of a path: The number of edges in a path is called the length of that path. In the above
diagram, the length of the path is 5.

Representation of Graphs in Data Structures


Graphs in data structures are used to represent the relationships between objects. Every
graph consists of a set of points known as vertices or nodes connected by lines known as
edges. The vertices in a network represent entities.

The most frequent graph representations are the two that follow:
• Adjacency matrix
• Adjacency list

Adjacency Matrix
• A sequential representation is an adjacency matrix.
• It's used to show which nodes are next to one another. I.e., is there any connection between
nodes in a graph?
• You create an M X M matrix G for this representation. If an edge exists between vertex a and
vertex b, the corresponding element of G, gi,j = 1, otherwise gi,j = 0.
• If there is a weighted graph, you can record the edge's weight instead of 1s and 0s.
IT07-IT07L Data Structure and Algorithm
7
Module 9: Graphs

Undirected Graph Representation

Directed Graph Representation


Weighted Undirected Graph Representation

Course Module
Adjacency List
• A linked representation is an adjacency list.
• You keep a list of neighbors for each vertex in the graph in this representation. It means that
each vertex in the graph has a list of its neighboring vertices.
• You have an array of vertices indexed by the vertex number, and the corresponding array
member for each vertex x points to a singly linked list of x's neighbors.

Directed Graph Representation Implemented Using Linked List

References and Supplementary Materials


Online Supplementary Reading Materials
1. Ravikiran A S. (Dec. 8, 2022). Your One-Stop Solution for Graphs In Data Structures.
Retrieved from: https://www.simplilearn.com/tutorials/data-structure
tutorial/graphs-in-data-structure
2. Tutorials Point. (n.d.). Graph Theory - Fundamentals. Retrieved from:
https://www.tutorialspoint.com/graph_theory/graph_theory_fundamentals.htm

You might also like