Professional Documents
Culture Documents
1715249104-graphs
1715249104-graphs
Graphs
• A graph is a data structure that consists of a set of vertices and a
set of edges between pairs of vertices
• Edges represent paths or connections between the vertices
• The set of vertices and the set of edges must both be finite and
neither one be empty
Graphs
• A graph is a pair (V, E), where
• V is a set of vertices
• E is a set of pairs of vertices, called edges
3
Edge Types
• Directed edge
• ordered pair of vertices (u,v)
• first vertex u is the origin A B
• second vertex v is the destination
• Undirected edge A B
• unordered pair of vertices (u,v)
4
Directed and Undirected Graphs
5
Weighted Graphs
6
Terminology
• End vertices (or endpoints) of
an edge
• U and V are the endpoints of
a V
• Edges incident on a vertex a b
• a, d, and b are incident on V h j
• Adjacent vertices U d X Z
• U and V are adjacent
c e i
• Degree of a vertex
• X has degree 5 W g
• Parallel edges
• h and i are parallel edges f
• Self-loop
Y
• j is a self-loop
7
Terminology (cont.)
8
Terminology (cont.)
9
Subgraphs
• A subgraph S of a graph G is a
graph such that
• The vertices of S are a subset of
the vertices of G
• The edges of S are a subset of
the edges of G
• A spanning subgraph of G is a Subgraph
subgraph that contains all the
vertices of G
Spanning subgraph
Graph
10
Spanning Trees
Spanning tree
11
Degree
The degree of a vertex is the number of edges
incident to that vertex
For directed graph,
– the in-degree of a vertex v is the number of edges
that have v as the head
– the out-degree of a vertex v is the number of edges
that have v as the tail
– if di is the degree of a vertex i in a graph G with n
vertices and e edges, the number of edges is
n 1
e( d )/ 2
0
i
12
Graph Representations
Adjacency Lists
Adjacency Matrix
13
Adjacency List
14
Adjacency List
1 2 5
1
2 1 5 4
5 2 3 4
4 2 3 5
4 3 5 4 1 2
1 2 3 4 5
1 0 1 0 0 1
1
2 1 0 0 1 1
3 0 0 0 1 0
5 2
4 0 1 1 0 1
4 5 1 1 0 1 0
3
16
Graph Traversal
1. Breadth-First Search
-----Queue
2. Depth-First Search
-----Stack
Pseudocode (BFS)
1. let Q be queue.
2. Q.enqueue( s )
3. mark s as visited.
4. while ( Q is not empty)
//Removing that vertex from queue,whose neighbour will be visited now
4.1 v = Q.dequeue( )
4.2 for all neighbours w of v in Graph G //processing all the
neighbours of v
4.3 if w is not visited
4.4 Q.enqueue( w ) //Stores w in Q to further visit its
neighbour
4.5 mark w as visited.
18
19
Example (BFS)
20
BFS Graph Traversal
BFS Graph Traversal
Pseudocode (DFS)
DFS (G, s): //Where G is graph and s is source vertex
23
Example (DFS)
24
25
DFS Graph Traversal
DFS Graph Traversal
Minimum Spanning Tree
31
33
34
Prim’s Algorithm
Consider the graph shown below.
Which of the following edges form the MST of the given graph using Prim’s
algorithm, starting from vertex 4.
a) (4-3)(5-3)(2-3)(1-2)
b) (4-3)(3-5)(5-1)(1-2)
c) (4-3)(3-5)(5-2)(1-5) 35
d) (4-3)(3-2)(2-1)(1-5)
36
37
Minimum Spanning Tree – Prim’s Algorithm
38
Minimum Spanning Tree – Prim’s Algorithm
39
Minimum Spanning Tree – Prim’s Algorithm
(Ans)
40
Minimum Spanning Tree – Kruskal’s Algorithm
41
42
43
Kruskal’s Algorithm
Consider the given graph.
What is the weight of the minimum spanning tree using the Kruskal’s
algorithm?
a) 24
b) 23
c) 15
44
d) 19
45
Minimum Spanning Tree – Kruskal’s Algorithm
Minimum Spanning Tree – Kruskal’s Algorithm
47
Single source shortest path algorithm -
Dijkstra’s Algorithm
48
50
Dijkstra’s Algorithm