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

Unit 6 Graphs Intro

Uploaded by

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

Unit 6 Graphs Intro

Uploaded by

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

Unit 6 - Graphs

Learning Outcomes
• To be able to explain graph and related terminologies.
• To be able to represent graph using adjacency matrix and adjacency
list.

2
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

3
Formal definition of graphs
• A graph G is defined as follows:
G=(V,E)
V(G): a finite, nonempty set of vertices
E(G): a set of edges (pairs of vertices)

4
Directed vs. undirected graphs
• When the edges in a graph have no direction, the graph is called
undirected

5
Directed vs. undirected graphs (cont.)
• When the edges in a graph have a direction, the graph is called
directed (or digraph)

6
Trees vs graphs
• Trees are special cases of graphs.

7
Graph terminology
• Adjacent nodes: two nodes are adjacent if they are connected by an
edge 5 is adjacent to 7
7 is adjacent from 5

• Path: a sequence of vertices that connect two nodes in a graph


• Complete graph: a graph in which every vertex is directly connected
to every other vertex

8
Graph terminology (cont.)
• What is the number of edges in a complete directed graph with N vertices?
N * (N-1)

9
Graph terminology (cont.)
• What is the number of edges in a complete undirected graph with N vertices?
N * (N-1) / 2

10
Graph terminology (cont.)
• Weighted graph: a graph in which each edge carries a value

11
Graph implementation
• Adjacency matrix representation
• An adjacency matrix is used to represent which nodes are adjacent to one another
• For any graph G having n nodes, the adjacency matrix will have the dimension of n * n.
• In an adjacency matrix, the rows and columns are labelled by graph vertices. An entry aij in the adjacency
matrix will contain 1, if vertices vi and vj are adjacent to each other.

12
Graph implementation
Adjacency matrix representation

13
Graph implementation
• Adjacency List Representation- An adjacency list is another way in which graphs can be represented in
the computer’s memory.
• This structure consists of a list of all nodes in G. Furthermore, every node is in turn linked to its own list that
contains the names of all other nodes that are adjacent to it.

14
Adjacency matrix vs. adjacency list
representation
• Adjacency matrix
• Good for dense graphs --|E|~O(|V|2)
• Memory requirements: O(|V| + |E| ) = O(|V|2 )
• Connectivity between two vertices can be tested quickly
• Adjacency list
• Good for sparse graphs -- |E|~O(|V|)
• Memory requirements: O(|V| + |E|)=O(|V|)
• Vertices adjacent to another vertex can be found quickly

15
Adjacency matrix
#define n 10
Int adj[n][n];
Initialize(int a[][])
{
int i,j;
For (i=0;i<n;i++)
For(j=0;j<n;j++)
a[i][j]=0;
}
Addedge(int I, int j)
{
}
Removeedge(int I, int j)
{
}
Int adjacent(int I, int j)
{…}

16
Adjacency list implementation
Typedef struct Node
{
Int vertex;
Struct node *next;
}node;
Node *head[n];
Addedge(int I, int j)
{
//create a new node
Node *Newnode=….
//insert a newnode in the linkedlist
If(head[i]==NULL)
head[i]=newnode;
Else
{
//traverse and insert at the end
…}

17
Thank You
Graph Traversal
• Traversing a graph is visiting each nodes exactly once in a particular order.
• The two standard methods of graph traversal methods are-
• 1. Breadth-first search
• 2. Depth-first search

19
Breadth first search (BFS)
• Visit all siblings of a node before visiting the children.
• It visits all nodes at the same level first and then goes to next level.
A G

BB D

C F E

•A B D G C F E

20
Algorithm - BFS
Step 1. Select the root node.
Step 2. Mark all nodes unvisited.
for (i=0; i<n; i++) visited[i]=0; A G
Step 3. Insert root node into the queue
insert(queue, root)
Step 4. while (queue not empty) BB D
{
u= delete(queue)
If (u is not visited) C F E
{
Mark u as visited, visited[u]=1 A
Print u;
Insert all adjacent nodes of u that are not visited. B D G
}
} D G C
G C

21
Depth first traversal (DFS)
• In DFS, all descendants of a node are visited before its unvisited
siblings.
• DFS traverses a single path of the graph until it visits a node with no
successor. It then resumes at the last node on the path just traversed
that has an unvisited successor and begins traversing a new path
A G
emanating from that node.
B D

C FF E

ABCDFEG

22
Algorithm - DFS
Step 1. Select the root node.
Step 2. Mark all nodes unvisited. A G
for (i=0; i<n; i++) visited[i]=0;
Step 3. Insert root node into the queue
push(stack, root) B D
Step 4. while (stack not empty)
{
u= pop(stack) C FF E
If (u is not visited)
{
Mark u as visited, visited[u]=1 ABCDFEG
Print u;
push all adjacent nodes of u that are not visited.
}
}

23
Topological Sorting
• A topological sort of a DAG G is an ordering of the vertices of G such that if G contains an edge (u, v),
then u appears before v in the ordering.
• Topological sorting is widely used in scheduling applications, jobs, or tasks.
• The jobs that must be completed are represented by nodes, and there is an edge from node u to v if job u
must be completed before job v can be started.

24
25
THANK YOU

26

You might also like