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

Lecture 12 Graph

A graph is a data structure consisting of vertices and edges that connect vertices and describe relationships; graphs can be undirected with edges having no direction or directed. Graphs can be represented using adjacency matrices with a 2D array showing connections between vertices, or adjacency lists with a collection of lists showing each vertex and its connections. Graphs are useful for modeling real-world networks like maps, social networks, and flight routes.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Lecture 12 Graph

A graph is a data structure consisting of vertices and edges that connect vertices and describe relationships; graphs can be undirected with edges having no direction or directed. Graphs can be represented using adjacency matrices with a 2D array showing connections between vertices, or adjacency lists with a collection of lists showing each vertex and its connections. Graphs are useful for modeling real-world networks like maps, social networks, and flight routes.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Graph

Afsana Begum
Senior Lecturer,
Software Engineering Department,
Daffodil International University
What is a graph?
• A data structure that consists of a set of nodes
(vertices) and a set of edges that relate the nodes
to each other
• The set of edges describes relationships among
the vertices
Undirected graphs

• When the edges in a graph have no direction, the graph is called


undirected
Directed Graph
Weighted graphs
• Here we have the following parts.
• The underlying set for the Vertices set is Integer.
• The underlying set for the weights is Integer.
• The Vertices set = {1,2,3,4,5}
• The Edge set = {(1,4,5) ,(4,5,58) ,(3,5,34) ,(2,4,5) ,(2,5,4) ,(3,2,14) ,
(1,2,2)}
Graph Representation
Vertices can be represent as one dimensional array.

Graph edges are commonly represented in two ways:

1. Adjacency Matrix
An adjacency matrix is 2D array of V x V vertices. Each row and
column represent a vertex.

If the value of any element a[i][j] is 1, it represents that there is an


edge connecting vertex i and vertex j.
Adjacency Matrix Example

Since it is an undirected graph, for edge (0,2), we also need


to mark edge (2,0); making the adjacency matrix symmetric
about the diagonal. Edge lookup(checking if an edge exists
between vertex A and vertex B) is extremely fast in
adjacency matrix representation but we have to reserve
space for every possible link between all vertices(V x V), so
it requires more space.
Adjacency Matrix for Flight Connections
2. Adjacency List
Adjacency list is a collection of unordered lists used to
represent a finite graph.
Adjacency List Representation of Graphs
Trees are special cases of graphs!
Terminology
• (a) A campus map as a graph;
(b) a subgraph
Terminology

• Graphs that are (a) connected;


(b) disconnected; and (c) complete
Terminology

• (a) A multigraph is not a simple graph;


(b) a self edge is not allowed in a simple graph
Graph terminology
• Path: A sequence of vertices that connects two nodes in a graph
• The length of a path is the number of edges in the path.

1 2

e.g., a path from 1 to 4


<1, 2, 3, 4>

3 4
Graph terminology
Complete graph: A graph in which every vertex is
directly connected to every other vertex
Graph Implementation
Directed Graph:

https://www.techiedelight.com/implement-graph-data-structure
-c/

Undirected graph:

https://www.geeksforgeeks.org/graph-and-its-representations/

You might also like