SlideShare a Scribd company logo
GRAPHS
DATA STRUCTURES
MUHAMMAD HAMMAD WASEEM
1
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
2
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)
3
DIRECTED VS. UNDIRECTED
GRAPHS
• When the edges in a graph have no direction, the
graph is called undirected.
• When the edges in a graph have a direction, the
graph is called directed (or digraph)
4
E(Graph2) = {(1,3) (3,1) (5,9) (9,11)
TREES VS. GRAPHS
• Trees are special cases of graphs!!
5
GRAPH TERMINOLOGY
• Adjacent nodes: two nodes are adjacent if they are connected
by an edge
• 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
• Degree of a vertex: The degree of a vertex in a graph is the
number of edges that touch it.
• The Size of a Graph: The size of a graph is the number of
vertices that it has.
6
5 is adjacent to 7
7 is adjacent from 5
GRAPH TERMINOLOGY
(CONT.)
• What is the number of edges in a
complete directed graph with N
vertices?
N * (N-1)
7
2
( )O N
GRAPH TERMINOLOGY
(CONT.)
• What is the number of edges in a
complete undirected graph with N
vertices?
N * (N-1) / 2
8
2
( )O N
GRAPH TERMINOLOGY
(CONT.)
• Weighted graph: a graph in which each
edge carries a value
9
GRAPH IMPLEMENTATION
• Array-based implementation
• A 1D array is used to represent the vertices
• A 2D array (adjacency matrix) is used to represent
the edges
10
ARRAY-BASED IMPLEMENTATION
11
GRAPH IMPLEMENTATION
(CONT.)
• Linked-list implementation
• A 1D array is used to represent the
vertices
• A list is used for each vertex v which
contains the vertices which are adjacent
from v (adjacency list)
12
LINKED-LIST IMPLEMENTATION
13
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
14
GRAPH SEARCHING
• Problem: find a path between two nodes of the
graph (e.g., Austin and Washington)
• Methods: Depth-First-Search (DFS) or Breadth-
First-Search (BFS)
15
DEPTH-FIRST-SEARCH (DFS)
• What is the idea behind DFS?
• Travel as far as you can down a path
• Back up as little as possible when you reach a "dead
end" (i.e., next vertex has been "marked" or there is
no next vertex)
• DFS can be implemented efficiently using a
stack
16
DEPTH-FIRST-SEARCH (DFS)
(CONT.)Set found to false
stack.Push(startVertex)
DO
stack.Pop(vertex)
IF vertex == endVertex
Set found to true
ELSE
Push all adjacent vertices onto stack
WHILE !stack.IsEmpty() AND !found
IF(!found)
Write "Path does not exist"
17
start end
(initialization)
18
19
20
BREADTH-FIRST-SEARCHING
(BFS)
• What is the idea behind BFS?
• Look at all possible paths at the same depth before
you go at a deeper level
• Back up as far as possible when you reach a "dead
end" (i.e., next vertex has been "marked" or there is
no next vertex)
21
BREADTH-FIRST-SEARCHING (BFS)
(CONT.)
• BFS can be implemented efficiently using
a queue
Set found to false
queue.Enqueue(startVertex)
DO
queue.Dequeue(vertex)
IF vertex == endVertex
Set found to true
ELSE
Enqueue all adjacent vertices onto queue
WHILE !queue.IsEmpty() AND !found
• Should we mark a vertex when it is
enqueued or when it is dequeued ? 22
IF(!found)
Write "Path does not exist"
start end
(initialization)
23
next:
24
25
SINGLE-SOURCE SHORTEST-
PATH PROBLEM
• There are multiple paths from a source vertex
to a destination vertex
• Shortest path: the path whose total weight
(i.e., sum of edge weights) is minimum
• Examples:
• Austin->Houston->Atlanta->Washington: 1560
miles
• Austin->Dallas->Denver->Atlanta->Washington:
2980 miles
26
SINGLE-SOURCE SHORTEST-
PATH PROBLEM (CONT.)
• Common algorithms: Dijkstra's algorithm,
Bellman-Ford algorithm
• BFS can be used to solve the shortest graph
problem when the graph is weightless or all
the weights are the same
(mark vertices before Enqueue)
27
SIMPLE SEARCH
ALGORITHM
Let S be the start state
1. Initialize Q with the start node Q=(S) as
only entry; set Visited = (S)
2. If Q is empty, fail. Else pick node X from
Q
3. If X is a goal, return X, we’ve reached the
goal
4. (Otherwise) Remove X from Q
5. Find all the children of node X not in
Visited
6. Add these to Q; Add Children of X to
Visited
7. Go to Step 2
28
DEPTH FIRST SEARCH(DFS)
• Expand deepest unexpanded node
• Implementation:
• fringe = LIFO queue, i.e., put successors at front
• Properties of DFS
• Complete: No, fails in infinite-depth spaces, spaces with
loops
• Modify to avoid repeated states along path
• Complete in finite spaces
• Time: O(bm): terrible if m is much larger than d
• but if solutions are dense, may be much faster than
breadth-first
• Space: O(bm), i.e., linear space!
29
DFS: EXAMPLE
Q Visited
1
2
3
4
5
30
S
BA
E FDC
G H
DFS: EXAMPLE
Q Visited
1 S S
2
3
4
5
31
S
BA
E FDC
G H
DFS: EXAMPLE
Q Visited
1 S S
2 A,B S,A,B
3
4
5
32
S
BA
E FDC
G H
DFS: EXAMPLE
Q Visited
1 S S
2 A,B S,A,B
3 C,D,B S,A,B,C,D
4
5
33
S
BA
E FDC
G H
DFS: EXAMPLE
Q Visited
1 S S
2 A,B S,A,B
3 C,D,B S,A,B,C,D
4 G,H,D,B S,A,B,C,D,G,H
5
34
S
BA
E FDC
G H
DFS: EXAMPLE
Q Visited
1 S S
2 A,B S,A,B
3 C,D,B S,A,B,C,D
4 G,H,D,B S,A,B,C,D,G,H
5 H,D,B S,A,B,C,D,G,H
35
S
BA
E FDC
G H
DFS: EXAMPLE
Q Visited
1 S S
2 A,B S,A,B
3 C,D,B S,A,B,C,D
4 G,H,D,B S,A,B,C,D,G,H
5 H,D,B S,A,B,C,D,G,H
6 D,B S,A,B,C,D,G,H
36
S
BA
E FDC
G H
DFS: EXAMPLE
Q Visited
1 S S
2 A,B S,A,B
3 C,D,B S,A,B,C,D
4 G,H,D,B S,A,B,C,D,G,H
5 H,D,B S,A,B,C,D,G,H
6 D,B S,A,B,C,D,G,H
37
S
BA
E FDC
G H
BREADTH FIRST SEARCH(BFS)
• Expand shallowest unexpanded node
• Implementation:
• fringe = FIFO queue, i.e., New successors go at end
• Properties of DFS
• Complete: Yes(if b is finite)
• Time: 1+b+b2+b3+… +bd + b(bd-1) = O(bd+1)
• Space: O(bd+1) (keeps every node in memory)
• Imagine searching a tree with branching factor 8 and depth 10.
Assume a node requires just 8 bytes of storage. The breadth first
search might require up to:
= (8)10 nodes
= (23)10 X 23 = 233 bytes
= 8,000 Mbytes
= 8 Gbytes
• Optimal: Yes (if cost = 1 per step)
38
BFS: EXAMPLE
Q Visited
1
2
3
4
5
39
S
BA
E FDC
G H
BFS: EXAMPLE
Q Visited
1 S S
2
3
4
5
40
S
BA
E FDC
G H
BFS: EXAMPLE
Q Visited
1 S S
2 A,B S,A,B
3
4
5
41
S
BA
E FDC
G H
BFS: EXAMPLE
Q Visited
1 S S
2 A,B S,A,B
3 B,C,D S,A,B,C,D
4
5
42
S
BA
E FDC
G H
BFS: EXAMPLE
Q Visited
1 S S
2 A,B S,A,B
3 B,C,D S,A,B,C,D
4
5
43
S
BA
E FDC
G H
BFS: EXAMPLE
Q Visited
1 S S
2 A,B S,A,B
3 B,C,D S,A,B,C,D
4 C,D,E,F S,A,B,C,D,E,F
5
44
S
BA
E FDC
G H
BFS: EXAMPLE
Q Visited
2 A,B S,A,B
3 B,C,D S,A,B,C,D
4 C,D,E,F S,A,B,C,D,E,F
5 D,E,F,G,H S,A,B,C,D,E,F,G,H
6
45
S
BA
E FDC
G H
BFS: EXAMPLE
Q Visited
3 B,C,D S,A,B,C,D
4 C,D,E,F S,A,B,C,D,E,F
5 D,E,F,G,H S,A,B,C,D,E,F,G,H
6 E,F,G,H S,A,B,C,D,E,F,G,H
7
46
S
BA
E FDC
G H
BFS: EXAMPLE
Q Visited
4 C,D,E,F S,A,B,C,D,E,F
5 D,E,F,G,H S,A,B,C,D,E,F,G,H
6 E,F,G,H S,A,B,C,D,E,F,G,H
7 F,G,H S,A,B,C,D,E,F,G,H
8
47
S
BA
E FDC
G H
BFS: EXAMPLE
Q Visited
5 D,E,F,G,H S,A,B,C,D,E,F,G,H
6 E,F,G,H S,A,B,C,D,E,F,G,H
7 F,G,H S,A,B,C,D,E,F,G,H
8 G,H S,A,B,C,D,E,F,G,H
9
48
S
BA
E FDC
G H
BFS: EXAMPLE
Q Visited
6 E,F,G,H S,A,B,C,D,E,F,G,H
7 F,G,H S,A,B,C,D,E,F,G,H
8 G,H S,A,B,C,D,E,F,G,H
9 H S,A,B,C,D,E,F,G,H
10
49
S
BA
E FDC
G H
BFS: EXAMPLE
Q Visited
6 E,F,G,H S,A,B,C,D,E,F,G,H
7 F,G,H S,A,B,C,D,E,F,G,H
8 G,H S,A,B,C,D,E,F,G,H
9 S,A,B,C,D,E,F,G,H
10 S,A,B,C,D,E,F,G,H
50
S
BA
E FDC
G H
BFS: EXAMPLE
Q Visited
1 S S
2 A,B S,A,B
3 B,C,D S,A,B,C,D
4 C,D,E,F S,A,B,C,D,E,F
5 D,E,F,G,H S,A,B,C,D,E,F,G,H
6 E,F,G,H S,A,B,C,D,E,F,G,H
7 F,G,H S,A,B,C,D,E,F,G,H
8 G,H S,A,B,C,D,E,F,G,H
9 H S,A,B,C,D,E,F,G,H
10 S,A,B,C,D,E,F,G,H
51
S
BA
E FDC
G H
THANKS
• Implement DFS and BFS in lab and submit
softcopy at m.hammad.wasim@gmail.com
52

More Related Content

What's hot

Tree traversal techniques
Tree traversal techniquesTree traversal techniques
Tree traversal techniques
Syed Zaid Irshad
 
Queue in Data Structure
Queue in Data Structure Queue in Data Structure
Queue in Data Structure
Janki Shah
 
Binary Tree Traversal
Binary Tree TraversalBinary Tree Traversal
Binary Tree Traversal
Dhrumil Panchal
 
Binary search tree(bst)
Binary search tree(bst)Binary search tree(bst)
Binary search tree(bst)
Hossain Md Shakhawat
 
Breadth First Search & Depth First Search
Breadth First Search & Depth First SearchBreadth First Search & Depth First Search
Breadth First Search & Depth First Search
Kevin Jadiya
 
Depth First Search ( DFS )
Depth First Search ( DFS )Depth First Search ( DFS )
Depth First Search ( DFS )
Sazzad Hossain
 
Binary search tree operations
Binary search tree operationsBinary search tree operations
Binary search tree operations
Kamran Zafar
 
1.1 binary tree
1.1 binary tree1.1 binary tree
1.1 binary tree
Krish_ver2
 
Lec 17 heap data structure
Lec 17 heap data structureLec 17 heap data structure
Lec 17 heap data structureSajid Marwat
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
Abhishek L.R
 
Data Structure and Algorithms Binary Search Tree
Data Structure and Algorithms Binary Search TreeData Structure and Algorithms Binary Search Tree
Data Structure and Algorithms Binary Search Tree
ManishPrajapati78
 
Hashing in datastructure
Hashing in datastructureHashing in datastructure
Hashing in datastructure
rajshreemuthiah
 
Bfs and Dfs
Bfs and DfsBfs and Dfs
Bfs and Dfs
Masud Parvaze
 
Threaded Binary Tree
Threaded Binary TreeThreaded Binary Tree
Threaded Binary Tree
khabbab_h
 
Stacks
StacksStacks
Stacks
sweta dargad
 
Trees
Trees Trees
Trees
Gaditek
 
1.5 binary search tree
1.5 binary search tree1.5 binary search tree
1.5 binary search tree
Krish_ver2
 
Graphs - Discrete Math
Graphs - Discrete MathGraphs - Discrete Math
Graphs - Discrete Math
Sikder Tahsin Al-Amin
 
Searching algorithms
Searching algorithmsSearching algorithms
Searching algorithms
Trupti Agrawal
 

What's hot (20)

Tree traversal techniques
Tree traversal techniquesTree traversal techniques
Tree traversal techniques
 
Queue in Data Structure
Queue in Data Structure Queue in Data Structure
Queue in Data Structure
 
Binary Tree Traversal
Binary Tree TraversalBinary Tree Traversal
Binary Tree Traversal
 
Binary search tree(bst)
Binary search tree(bst)Binary search tree(bst)
Binary search tree(bst)
 
Breadth First Search & Depth First Search
Breadth First Search & Depth First SearchBreadth First Search & Depth First Search
Breadth First Search & Depth First Search
 
Depth First Search ( DFS )
Depth First Search ( DFS )Depth First Search ( DFS )
Depth First Search ( DFS )
 
Binary search tree operations
Binary search tree operationsBinary search tree operations
Binary search tree operations
 
1.1 binary tree
1.1 binary tree1.1 binary tree
1.1 binary tree
 
Lec 17 heap data structure
Lec 17 heap data structureLec 17 heap data structure
Lec 17 heap data structure
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
Data Structure and Algorithms Binary Search Tree
Data Structure and Algorithms Binary Search TreeData Structure and Algorithms Binary Search Tree
Data Structure and Algorithms Binary Search Tree
 
Hashing in datastructure
Hashing in datastructureHashing in datastructure
Hashing in datastructure
 
Bfs and Dfs
Bfs and DfsBfs and Dfs
Bfs and Dfs
 
Heaps
HeapsHeaps
Heaps
 
Threaded Binary Tree
Threaded Binary TreeThreaded Binary Tree
Threaded Binary Tree
 
Stacks
StacksStacks
Stacks
 
Trees
Trees Trees
Trees
 
1.5 binary search tree
1.5 binary search tree1.5 binary search tree
1.5 binary search tree
 
Graphs - Discrete Math
Graphs - Discrete MathGraphs - Discrete Math
Graphs - Discrete Math
 
Searching algorithms
Searching algorithmsSearching algorithms
Searching algorithms
 

Viewers also liked

Graphs In Data Structure
Graphs In Data StructureGraphs In Data Structure
Graphs In Data StructureAnuj Modi
 
Lecture8 data structure(graph)
Lecture8 data structure(graph)Lecture8 data structure(graph)
Data structure computer graphs
Data structure computer graphsData structure computer graphs
Data structure computer graphsKumar
 
Graph data structure
Graph data structureGraph data structure
Graph data structureTech_MX
 
Matrix Representation Of Graph
Matrix Representation Of GraphMatrix Representation Of Graph
Matrix Representation Of GraphAbhishek Pachisia
 
Graphs in data structure
Graphs in data structureGraphs in data structure
Graphs in data structure
hamza javed
 
17. Trees and Graphs
17. Trees and Graphs17. Trees and Graphs
17. Trees and Graphs
Intro C# Book
 
DATA STRUCTURES
DATA STRUCTURESDATA STRUCTURES
DATA STRUCTURES
bca2010
 
Graphs in Data Structure
 Graphs in Data Structure Graphs in Data Structure
Graphs in Data Structure
hafsa komal
 
Graphs data Structure
Graphs data StructureGraphs data Structure
Graphs data Structure
Mahmoud Alfarra
 
Data structure & its types
Data structure & its typesData structure & its types
Data structure & its types
Rameesha Sadaqat
 
Graph Basic In Data structure
Graph Basic In Data structureGraph Basic In Data structure
Graph Basic In Data structure
Ikhlas Rahman
 
Data structure and its types
Data structure and its typesData structure and its types
Data structure and its typesNavtar Sidhu Brar
 
Trees data structure
Trees data structureTrees data structure
Trees data structure
Sumit Gupta
 
Depth First Search and Breadth First Search
Depth First Search and Breadth First SearchDepth First Search and Breadth First Search
Depth First Search and Breadth First Search
Benjamin Sach
 
Preparation Data Structures 11 graphs
Preparation Data Structures 11 graphsPreparation Data Structures 11 graphs
Preparation Data Structures 11 graphs
Andres Mendez-Vazquez
 
Data Structures, Graphs
Data Structures, GraphsData Structures, Graphs
Data Structures, GraphsJibrael Jos
 
Graphss
GraphssGraphss
Graphss
fika sweety
 
Data structure introduction
Data structure introductionData structure introduction
Data structure introduction
ramyasanthosh
 
Graphs in data structures
Graphs in data structuresGraphs in data structures
Graphs in data structures
Savit Chandra
 

Viewers also liked (20)

Graphs In Data Structure
Graphs In Data StructureGraphs In Data Structure
Graphs In Data Structure
 
Lecture8 data structure(graph)
Lecture8 data structure(graph)Lecture8 data structure(graph)
Lecture8 data structure(graph)
 
Data structure computer graphs
Data structure computer graphsData structure computer graphs
Data structure computer graphs
 
Graph data structure
Graph data structureGraph data structure
Graph data structure
 
Matrix Representation Of Graph
Matrix Representation Of GraphMatrix Representation Of Graph
Matrix Representation Of Graph
 
Graphs in data structure
Graphs in data structureGraphs in data structure
Graphs in data structure
 
17. Trees and Graphs
17. Trees and Graphs17. Trees and Graphs
17. Trees and Graphs
 
DATA STRUCTURES
DATA STRUCTURESDATA STRUCTURES
DATA STRUCTURES
 
Graphs in Data Structure
 Graphs in Data Structure Graphs in Data Structure
Graphs in Data Structure
 
Graphs data Structure
Graphs data StructureGraphs data Structure
Graphs data Structure
 
Data structure & its types
Data structure & its typesData structure & its types
Data structure & its types
 
Graph Basic In Data structure
Graph Basic In Data structureGraph Basic In Data structure
Graph Basic In Data structure
 
Data structure and its types
Data structure and its typesData structure and its types
Data structure and its types
 
Trees data structure
Trees data structureTrees data structure
Trees data structure
 
Depth First Search and Breadth First Search
Depth First Search and Breadth First SearchDepth First Search and Breadth First Search
Depth First Search and Breadth First Search
 
Preparation Data Structures 11 graphs
Preparation Data Structures 11 graphsPreparation Data Structures 11 graphs
Preparation Data Structures 11 graphs
 
Data Structures, Graphs
Data Structures, GraphsData Structures, Graphs
Data Structures, Graphs
 
Graphss
GraphssGraphss
Graphss
 
Data structure introduction
Data structure introductionData structure introduction
Data structure introduction
 
Graphs in data structures
Graphs in data structuresGraphs in data structures
Graphs in data structures
 

Similar to Data Structures - Lecture 10 [Graphs]

Unit 9 graph
Unit   9 graphUnit   9 graph
Unit 9 graph
Dabbal Singh Mahara
 
LEC 12-DSALGO-GRAPHS(final12).pdf
LEC 12-DSALGO-GRAPHS(final12).pdfLEC 12-DSALGO-GRAPHS(final12).pdf
LEC 12-DSALGO-GRAPHS(final12).pdf
MuhammadUmerIhtisham
 
Unit ix graph
Unit   ix    graph Unit   ix    graph
Unit ix graph
Tribhuvan University
 
22-graphs1-dfs-bfs.ppt odiehehei7hoh97ho7bi6vi6go7gp
22-graphs1-dfs-bfs.ppt odiehehei7hoh97ho7bi6vi6go7gp22-graphs1-dfs-bfs.ppt odiehehei7hoh97ho7bi6vi6go7gp
22-graphs1-dfs-bfs.ppt odiehehei7hoh97ho7bi6vi6go7gp
chandrashekarr799
 
22-graphs1-dfs-bfs.ppt
22-graphs1-dfs-bfs.ppt22-graphs1-dfs-bfs.ppt
22-graphs1-dfs-bfs.ppt
KarunaBiswas3
 
U1 L5 DAA.pdf
U1 L5 DAA.pdfU1 L5 DAA.pdf
U1 L5 DAA.pdf
LakshyaBaliyan2
 
Graph Data Structure
Graph Data StructureGraph Data Structure
Graph Data Structure
Afaq Mansoor Khan
 
Breadth first search
Breadth first searchBreadth first search
Breadth first search
Sazzad Hossain
 
Unit-6 Graph.ppsx ppt
Unit-6 Graph.ppsx                                       pptUnit-6 Graph.ppsx                                       ppt
Unit-6 Graph.ppsx ppt
DhruvilSTATUS
 
lecture 17
lecture 17lecture 17
lecture 17sajinsc
 
Graphs
GraphsGraphs
Graphs
Dwight Sabio
 
Graphs
GraphsGraphs
Graphs
LavanyaJ28
 
graphin-c1.pnggraphin-c1.txt1 22 3 83 44 5.docx
graphin-c1.pnggraphin-c1.txt1 22 3 83 44 5.docxgraphin-c1.pnggraphin-c1.txt1 22 3 83 44 5.docx
graphin-c1.pnggraphin-c1.txt1 22 3 83 44 5.docx
whittemorelucilla
 
Graphical reprsentation dsa (DATA STRUCTURE ALGORITHM)
Graphical reprsentation dsa (DATA STRUCTURE ALGORITHM)Graphical reprsentation dsa (DATA STRUCTURE ALGORITHM)
Graphical reprsentation dsa (DATA STRUCTURE ALGORITHM)
abdulrafaychaudhry
 
Unit 2: All
Unit 2: AllUnit 2: All
Unit 2: All
Hector Zenil
 
Unit II_Graph.pptxkgjrekjgiojtoiejhgnltegjte
Unit II_Graph.pptxkgjrekjgiojtoiejhgnltegjteUnit II_Graph.pptxkgjrekjgiojtoiejhgnltegjte
Unit II_Graph.pptxkgjrekjgiojtoiejhgnltegjte
pournima055
 
Graph 1
Graph 1Graph 1
Lecture 16 - Dijkstra's Algorithm.pdf
Lecture 16 - Dijkstra's Algorithm.pdfLecture 16 - Dijkstra's Algorithm.pdf
Lecture 16 - Dijkstra's Algorithm.pdf
iftakhar8
 
Algorithms and data Chapter 3 V Graph.pptx
Algorithms and data Chapter 3 V Graph.pptxAlgorithms and data Chapter 3 V Graph.pptx
Algorithms and data Chapter 3 V Graph.pptx
zerihunnana
 
6. Graphs
6. Graphs6. Graphs
6. Graphs
Mandeep Singh
 

Similar to Data Structures - Lecture 10 [Graphs] (20)

Unit 9 graph
Unit   9 graphUnit   9 graph
Unit 9 graph
 
LEC 12-DSALGO-GRAPHS(final12).pdf
LEC 12-DSALGO-GRAPHS(final12).pdfLEC 12-DSALGO-GRAPHS(final12).pdf
LEC 12-DSALGO-GRAPHS(final12).pdf
 
Unit ix graph
Unit   ix    graph Unit   ix    graph
Unit ix graph
 
22-graphs1-dfs-bfs.ppt odiehehei7hoh97ho7bi6vi6go7gp
22-graphs1-dfs-bfs.ppt odiehehei7hoh97ho7bi6vi6go7gp22-graphs1-dfs-bfs.ppt odiehehei7hoh97ho7bi6vi6go7gp
22-graphs1-dfs-bfs.ppt odiehehei7hoh97ho7bi6vi6go7gp
 
22-graphs1-dfs-bfs.ppt
22-graphs1-dfs-bfs.ppt22-graphs1-dfs-bfs.ppt
22-graphs1-dfs-bfs.ppt
 
U1 L5 DAA.pdf
U1 L5 DAA.pdfU1 L5 DAA.pdf
U1 L5 DAA.pdf
 
Graph Data Structure
Graph Data StructureGraph Data Structure
Graph Data Structure
 
Breadth first search
Breadth first searchBreadth first search
Breadth first search
 
Unit-6 Graph.ppsx ppt
Unit-6 Graph.ppsx                                       pptUnit-6 Graph.ppsx                                       ppt
Unit-6 Graph.ppsx ppt
 
lecture 17
lecture 17lecture 17
lecture 17
 
Graphs
GraphsGraphs
Graphs
 
Graphs
GraphsGraphs
Graphs
 
graphin-c1.pnggraphin-c1.txt1 22 3 83 44 5.docx
graphin-c1.pnggraphin-c1.txt1 22 3 83 44 5.docxgraphin-c1.pnggraphin-c1.txt1 22 3 83 44 5.docx
graphin-c1.pnggraphin-c1.txt1 22 3 83 44 5.docx
 
Graphical reprsentation dsa (DATA STRUCTURE ALGORITHM)
Graphical reprsentation dsa (DATA STRUCTURE ALGORITHM)Graphical reprsentation dsa (DATA STRUCTURE ALGORITHM)
Graphical reprsentation dsa (DATA STRUCTURE ALGORITHM)
 
Unit 2: All
Unit 2: AllUnit 2: All
Unit 2: All
 
Unit II_Graph.pptxkgjrekjgiojtoiejhgnltegjte
Unit II_Graph.pptxkgjrekjgiojtoiejhgnltegjteUnit II_Graph.pptxkgjrekjgiojtoiejhgnltegjte
Unit II_Graph.pptxkgjrekjgiojtoiejhgnltegjte
 
Graph 1
Graph 1Graph 1
Graph 1
 
Lecture 16 - Dijkstra's Algorithm.pdf
Lecture 16 - Dijkstra's Algorithm.pdfLecture 16 - Dijkstra's Algorithm.pdf
Lecture 16 - Dijkstra's Algorithm.pdf
 
Algorithms and data Chapter 3 V Graph.pptx
Algorithms and data Chapter 3 V Graph.pptxAlgorithms and data Chapter 3 V Graph.pptx
Algorithms and data Chapter 3 V Graph.pptx
 
6. Graphs
6. Graphs6. Graphs
6. Graphs
 

More from Muhammad Hammad Waseem

[ITP - Lecture 17] Strings in C/C++
[ITP - Lecture 17] Strings in C/C++[ITP - Lecture 17] Strings in C/C++
[ITP - Lecture 17] Strings in C/C++
Muhammad Hammad Waseem
 
[ITP - Lecture 16] Structures in C/C++
[ITP - Lecture 16] Structures in C/C++[ITP - Lecture 16] Structures in C/C++
[ITP - Lecture 16] Structures in C/C++
Muhammad Hammad Waseem
 
[ITP - Lecture 15] Arrays & its Types
[ITP - Lecture 15] Arrays & its Types[ITP - Lecture 15] Arrays & its Types
[ITP - Lecture 15] Arrays & its Types
Muhammad Hammad Waseem
 
[ITP - Lecture 14] Recursion
[ITP - Lecture 14] Recursion[ITP - Lecture 14] Recursion
[ITP - Lecture 14] Recursion
Muhammad Hammad Waseem
 
[ITP - Lecture 13] Introduction to Pointers
[ITP - Lecture 13] Introduction to Pointers[ITP - Lecture 13] Introduction to Pointers
[ITP - Lecture 13] Introduction to Pointers
Muhammad Hammad Waseem
 
[ITP - Lecture 12] Functions in C/C++
[ITP - Lecture 12] Functions in C/C++[ITP - Lecture 12] Functions in C/C++
[ITP - Lecture 12] Functions in C/C++
Muhammad Hammad Waseem
 
[ITP - Lecture 11] Loops in C/C++
[ITP - Lecture 11] Loops in C/C++[ITP - Lecture 11] Loops in C/C++
[ITP - Lecture 11] Loops in C/C++
Muhammad Hammad Waseem
 
[ITP - Lecture 10] Switch Statement, Break and Continue Statement in C/C++
[ITP - Lecture 10] Switch Statement, Break and Continue Statement in C/C++[ITP - Lecture 10] Switch Statement, Break and Continue Statement in C/C++
[ITP - Lecture 10] Switch Statement, Break and Continue Statement in C/C++
Muhammad Hammad Waseem
 
[ITP - Lecture 09] Conditional Operator in C/C++
[ITP - Lecture 09] Conditional Operator in C/C++[ITP - Lecture 09] Conditional Operator in C/C++
[ITP - Lecture 09] Conditional Operator in C/C++
Muhammad Hammad Waseem
 
[ITP - Lecture 08] Decision Control Structures (If Statement)
[ITP - Lecture 08] Decision Control Structures (If Statement)[ITP - Lecture 08] Decision Control Structures (If Statement)
[ITP - Lecture 08] Decision Control Structures (If Statement)
Muhammad Hammad Waseem
 
[ITP - Lecture 07] Comments in C/C++
[ITP - Lecture 07] Comments in C/C++[ITP - Lecture 07] Comments in C/C++
[ITP - Lecture 07] Comments in C/C++
Muhammad Hammad Waseem
 
[ITP - Lecture 06] Operators, Arithmetic Expression and Order of Precedence
[ITP - Lecture 06] Operators, Arithmetic Expression and Order of Precedence[ITP - Lecture 06] Operators, Arithmetic Expression and Order of Precedence
[ITP - Lecture 06] Operators, Arithmetic Expression and Order of Precedence
Muhammad Hammad Waseem
 
[ITP - Lecture 05] Datatypes
[ITP - Lecture 05] Datatypes[ITP - Lecture 05] Datatypes
[ITP - Lecture 05] Datatypes
Muhammad Hammad Waseem
 
[ITP - Lecture 04] Variables and Constants in C/C++
[ITP - Lecture 04] Variables and Constants in C/C++[ITP - Lecture 04] Variables and Constants in C/C++
[ITP - Lecture 04] Variables and Constants in C/C++
Muhammad Hammad Waseem
 
[ITP - Lecture 03] Introduction to C/C++
[ITP - Lecture 03] Introduction to C/C++[ITP - Lecture 03] Introduction to C/C++
[ITP - Lecture 03] Introduction to C/C++
Muhammad Hammad Waseem
 
[ITP - Lecture 02] Steps to Create Program & Approaches of Programming
[ITP - Lecture 02] Steps to Create Program & Approaches of Programming[ITP - Lecture 02] Steps to Create Program & Approaches of Programming
[ITP - Lecture 02] Steps to Create Program & Approaches of Programming
Muhammad Hammad Waseem
 
[ITP - Lecture 01] Introduction to Programming & Different Programming Languages
[ITP - Lecture 01] Introduction to Programming & Different Programming Languages[ITP - Lecture 01] Introduction to Programming & Different Programming Languages
[ITP - Lecture 01] Introduction to Programming & Different Programming Languages
Muhammad Hammad Waseem
 
[OOP - Lec 20,21] Inheritance
[OOP - Lec 20,21] Inheritance[OOP - Lec 20,21] Inheritance
[OOP - Lec 20,21] Inheritance
Muhammad Hammad Waseem
 
[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions
Muhammad Hammad Waseem
 
[OOP - Lec 18] Static Data Member
[OOP - Lec 18] Static Data Member[OOP - Lec 18] Static Data Member
[OOP - Lec 18] Static Data Member
Muhammad Hammad Waseem
 

More from Muhammad Hammad Waseem (20)

[ITP - Lecture 17] Strings in C/C++
[ITP - Lecture 17] Strings in C/C++[ITP - Lecture 17] Strings in C/C++
[ITP - Lecture 17] Strings in C/C++
 
[ITP - Lecture 16] Structures in C/C++
[ITP - Lecture 16] Structures in C/C++[ITP - Lecture 16] Structures in C/C++
[ITP - Lecture 16] Structures in C/C++
 
[ITP - Lecture 15] Arrays & its Types
[ITP - Lecture 15] Arrays & its Types[ITP - Lecture 15] Arrays & its Types
[ITP - Lecture 15] Arrays & its Types
 
[ITP - Lecture 14] Recursion
[ITP - Lecture 14] Recursion[ITP - Lecture 14] Recursion
[ITP - Lecture 14] Recursion
 
[ITP - Lecture 13] Introduction to Pointers
[ITP - Lecture 13] Introduction to Pointers[ITP - Lecture 13] Introduction to Pointers
[ITP - Lecture 13] Introduction to Pointers
 
[ITP - Lecture 12] Functions in C/C++
[ITP - Lecture 12] Functions in C/C++[ITP - Lecture 12] Functions in C/C++
[ITP - Lecture 12] Functions in C/C++
 
[ITP - Lecture 11] Loops in C/C++
[ITP - Lecture 11] Loops in C/C++[ITP - Lecture 11] Loops in C/C++
[ITP - Lecture 11] Loops in C/C++
 
[ITP - Lecture 10] Switch Statement, Break and Continue Statement in C/C++
[ITP - Lecture 10] Switch Statement, Break and Continue Statement in C/C++[ITP - Lecture 10] Switch Statement, Break and Continue Statement in C/C++
[ITP - Lecture 10] Switch Statement, Break and Continue Statement in C/C++
 
[ITP - Lecture 09] Conditional Operator in C/C++
[ITP - Lecture 09] Conditional Operator in C/C++[ITP - Lecture 09] Conditional Operator in C/C++
[ITP - Lecture 09] Conditional Operator in C/C++
 
[ITP - Lecture 08] Decision Control Structures (If Statement)
[ITP - Lecture 08] Decision Control Structures (If Statement)[ITP - Lecture 08] Decision Control Structures (If Statement)
[ITP - Lecture 08] Decision Control Structures (If Statement)
 
[ITP - Lecture 07] Comments in C/C++
[ITP - Lecture 07] Comments in C/C++[ITP - Lecture 07] Comments in C/C++
[ITP - Lecture 07] Comments in C/C++
 
[ITP - Lecture 06] Operators, Arithmetic Expression and Order of Precedence
[ITP - Lecture 06] Operators, Arithmetic Expression and Order of Precedence[ITP - Lecture 06] Operators, Arithmetic Expression and Order of Precedence
[ITP - Lecture 06] Operators, Arithmetic Expression and Order of Precedence
 
[ITP - Lecture 05] Datatypes
[ITP - Lecture 05] Datatypes[ITP - Lecture 05] Datatypes
[ITP - Lecture 05] Datatypes
 
[ITP - Lecture 04] Variables and Constants in C/C++
[ITP - Lecture 04] Variables and Constants in C/C++[ITP - Lecture 04] Variables and Constants in C/C++
[ITP - Lecture 04] Variables and Constants in C/C++
 
[ITP - Lecture 03] Introduction to C/C++
[ITP - Lecture 03] Introduction to C/C++[ITP - Lecture 03] Introduction to C/C++
[ITP - Lecture 03] Introduction to C/C++
 
[ITP - Lecture 02] Steps to Create Program & Approaches of Programming
[ITP - Lecture 02] Steps to Create Program & Approaches of Programming[ITP - Lecture 02] Steps to Create Program & Approaches of Programming
[ITP - Lecture 02] Steps to Create Program & Approaches of Programming
 
[ITP - Lecture 01] Introduction to Programming & Different Programming Languages
[ITP - Lecture 01] Introduction to Programming & Different Programming Languages[ITP - Lecture 01] Introduction to Programming & Different Programming Languages
[ITP - Lecture 01] Introduction to Programming & Different Programming Languages
 
[OOP - Lec 20,21] Inheritance
[OOP - Lec 20,21] Inheritance[OOP - Lec 20,21] Inheritance
[OOP - Lec 20,21] Inheritance
 
[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions
 
[OOP - Lec 18] Static Data Member
[OOP - Lec 18] Static Data Member[OOP - Lec 18] Static Data Member
[OOP - Lec 18] Static Data Member
 

Recently uploaded

Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
TechSoup
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
Jheel Barad
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
JosvitaDsouza2
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
Levi Shapiro
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
Peter Windle
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
Delapenabediema
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
TechSoup
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
Nguyen Thanh Tu Collection
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Atul Kumar Singh
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
MIRIAMSALINAS13
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
EverAndrsGuerraGuerr
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
heathfieldcps1
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
DhatriParmar
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
camakaiclarkmusic
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
Jisc
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
MysoreMuleSoftMeetup
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
kaushalkr1407
 

Recently uploaded (20)

Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
 

Data Structures - Lecture 10 [Graphs]

  • 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 2
  • 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) 3
  • 4. DIRECTED VS. UNDIRECTED GRAPHS • When the edges in a graph have no direction, the graph is called undirected. • When the edges in a graph have a direction, the graph is called directed (or digraph) 4 E(Graph2) = {(1,3) (3,1) (5,9) (9,11)
  • 5. TREES VS. GRAPHS • Trees are special cases of graphs!! 5
  • 6. GRAPH TERMINOLOGY • Adjacent nodes: two nodes are adjacent if they are connected by an edge • 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 • Degree of a vertex: The degree of a vertex in a graph is the number of edges that touch it. • The Size of a Graph: The size of a graph is the number of vertices that it has. 6 5 is adjacent to 7 7 is adjacent from 5
  • 7. GRAPH TERMINOLOGY (CONT.) • What is the number of edges in a complete directed graph with N vertices? N * (N-1) 7 2 ( )O N
  • 8. GRAPH TERMINOLOGY (CONT.) • What is the number of edges in a complete undirected graph with N vertices? N * (N-1) / 2 8 2 ( )O N
  • 9. GRAPH TERMINOLOGY (CONT.) • Weighted graph: a graph in which each edge carries a value 9
  • 10. GRAPH IMPLEMENTATION • Array-based implementation • A 1D array is used to represent the vertices • A 2D array (adjacency matrix) is used to represent the edges 10
  • 12. GRAPH IMPLEMENTATION (CONT.) • Linked-list implementation • A 1D array is used to represent the vertices • A list is used for each vertex v which contains the vertices which are adjacent from v (adjacency list) 12
  • 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 14
  • 15. GRAPH SEARCHING • Problem: find a path between two nodes of the graph (e.g., Austin and Washington) • Methods: Depth-First-Search (DFS) or Breadth- First-Search (BFS) 15
  • 16. DEPTH-FIRST-SEARCH (DFS) • What is the idea behind DFS? • Travel as far as you can down a path • Back up as little as possible when you reach a "dead end" (i.e., next vertex has been "marked" or there is no next vertex) • DFS can be implemented efficiently using a stack 16
  • 17. DEPTH-FIRST-SEARCH (DFS) (CONT.)Set found to false stack.Push(startVertex) DO stack.Pop(vertex) IF vertex == endVertex Set found to true ELSE Push all adjacent vertices onto stack WHILE !stack.IsEmpty() AND !found IF(!found) Write "Path does not exist" 17
  • 19. 19
  • 20. 20
  • 21. BREADTH-FIRST-SEARCHING (BFS) • What is the idea behind BFS? • Look at all possible paths at the same depth before you go at a deeper level • Back up as far as possible when you reach a "dead end" (i.e., next vertex has been "marked" or there is no next vertex) 21
  • 22. BREADTH-FIRST-SEARCHING (BFS) (CONT.) • BFS can be implemented efficiently using a queue Set found to false queue.Enqueue(startVertex) DO queue.Dequeue(vertex) IF vertex == endVertex Set found to true ELSE Enqueue all adjacent vertices onto queue WHILE !queue.IsEmpty() AND !found • Should we mark a vertex when it is enqueued or when it is dequeued ? 22 IF(!found) Write "Path does not exist"
  • 25. 25
  • 26. SINGLE-SOURCE SHORTEST- PATH PROBLEM • There are multiple paths from a source vertex to a destination vertex • Shortest path: the path whose total weight (i.e., sum of edge weights) is minimum • Examples: • Austin->Houston->Atlanta->Washington: 1560 miles • Austin->Dallas->Denver->Atlanta->Washington: 2980 miles 26
  • 27. SINGLE-SOURCE SHORTEST- PATH PROBLEM (CONT.) • Common algorithms: Dijkstra's algorithm, Bellman-Ford algorithm • BFS can be used to solve the shortest graph problem when the graph is weightless or all the weights are the same (mark vertices before Enqueue) 27
  • 28. SIMPLE SEARCH ALGORITHM Let S be the start state 1. Initialize Q with the start node Q=(S) as only entry; set Visited = (S) 2. If Q is empty, fail. Else pick node X from Q 3. If X is a goal, return X, we’ve reached the goal 4. (Otherwise) Remove X from Q 5. Find all the children of node X not in Visited 6. Add these to Q; Add Children of X to Visited 7. Go to Step 2 28
  • 29. DEPTH FIRST SEARCH(DFS) • Expand deepest unexpanded node • Implementation: • fringe = LIFO queue, i.e., put successors at front • Properties of DFS • Complete: No, fails in infinite-depth spaces, spaces with loops • Modify to avoid repeated states along path • Complete in finite spaces • Time: O(bm): terrible if m is much larger than d • but if solutions are dense, may be much faster than breadth-first • Space: O(bm), i.e., linear space! 29
  • 31. DFS: EXAMPLE Q Visited 1 S S 2 3 4 5 31 S BA E FDC G H
  • 32. DFS: EXAMPLE Q Visited 1 S S 2 A,B S,A,B 3 4 5 32 S BA E FDC G H
  • 33. DFS: EXAMPLE Q Visited 1 S S 2 A,B S,A,B 3 C,D,B S,A,B,C,D 4 5 33 S BA E FDC G H
  • 34. DFS: EXAMPLE Q Visited 1 S S 2 A,B S,A,B 3 C,D,B S,A,B,C,D 4 G,H,D,B S,A,B,C,D,G,H 5 34 S BA E FDC G H
  • 35. DFS: EXAMPLE Q Visited 1 S S 2 A,B S,A,B 3 C,D,B S,A,B,C,D 4 G,H,D,B S,A,B,C,D,G,H 5 H,D,B S,A,B,C,D,G,H 35 S BA E FDC G H
  • 36. DFS: EXAMPLE Q Visited 1 S S 2 A,B S,A,B 3 C,D,B S,A,B,C,D 4 G,H,D,B S,A,B,C,D,G,H 5 H,D,B S,A,B,C,D,G,H 6 D,B S,A,B,C,D,G,H 36 S BA E FDC G H
  • 37. DFS: EXAMPLE Q Visited 1 S S 2 A,B S,A,B 3 C,D,B S,A,B,C,D 4 G,H,D,B S,A,B,C,D,G,H 5 H,D,B S,A,B,C,D,G,H 6 D,B S,A,B,C,D,G,H 37 S BA E FDC G H
  • 38. BREADTH FIRST SEARCH(BFS) • Expand shallowest unexpanded node • Implementation: • fringe = FIFO queue, i.e., New successors go at end • Properties of DFS • Complete: Yes(if b is finite) • Time: 1+b+b2+b3+… +bd + b(bd-1) = O(bd+1) • Space: O(bd+1) (keeps every node in memory) • Imagine searching a tree with branching factor 8 and depth 10. Assume a node requires just 8 bytes of storage. The breadth first search might require up to: = (8)10 nodes = (23)10 X 23 = 233 bytes = 8,000 Mbytes = 8 Gbytes • Optimal: Yes (if cost = 1 per step) 38
  • 40. BFS: EXAMPLE Q Visited 1 S S 2 3 4 5 40 S BA E FDC G H
  • 41. BFS: EXAMPLE Q Visited 1 S S 2 A,B S,A,B 3 4 5 41 S BA E FDC G H
  • 42. BFS: EXAMPLE Q Visited 1 S S 2 A,B S,A,B 3 B,C,D S,A,B,C,D 4 5 42 S BA E FDC G H
  • 43. BFS: EXAMPLE Q Visited 1 S S 2 A,B S,A,B 3 B,C,D S,A,B,C,D 4 5 43 S BA E FDC G H
  • 44. BFS: EXAMPLE Q Visited 1 S S 2 A,B S,A,B 3 B,C,D S,A,B,C,D 4 C,D,E,F S,A,B,C,D,E,F 5 44 S BA E FDC G H
  • 45. BFS: EXAMPLE Q Visited 2 A,B S,A,B 3 B,C,D S,A,B,C,D 4 C,D,E,F S,A,B,C,D,E,F 5 D,E,F,G,H S,A,B,C,D,E,F,G,H 6 45 S BA E FDC G H
  • 46. BFS: EXAMPLE Q Visited 3 B,C,D S,A,B,C,D 4 C,D,E,F S,A,B,C,D,E,F 5 D,E,F,G,H S,A,B,C,D,E,F,G,H 6 E,F,G,H S,A,B,C,D,E,F,G,H 7 46 S BA E FDC G H
  • 47. BFS: EXAMPLE Q Visited 4 C,D,E,F S,A,B,C,D,E,F 5 D,E,F,G,H S,A,B,C,D,E,F,G,H 6 E,F,G,H S,A,B,C,D,E,F,G,H 7 F,G,H S,A,B,C,D,E,F,G,H 8 47 S BA E FDC G H
  • 48. BFS: EXAMPLE Q Visited 5 D,E,F,G,H S,A,B,C,D,E,F,G,H 6 E,F,G,H S,A,B,C,D,E,F,G,H 7 F,G,H S,A,B,C,D,E,F,G,H 8 G,H S,A,B,C,D,E,F,G,H 9 48 S BA E FDC G H
  • 49. BFS: EXAMPLE Q Visited 6 E,F,G,H S,A,B,C,D,E,F,G,H 7 F,G,H S,A,B,C,D,E,F,G,H 8 G,H S,A,B,C,D,E,F,G,H 9 H S,A,B,C,D,E,F,G,H 10 49 S BA E FDC G H
  • 50. BFS: EXAMPLE Q Visited 6 E,F,G,H S,A,B,C,D,E,F,G,H 7 F,G,H S,A,B,C,D,E,F,G,H 8 G,H S,A,B,C,D,E,F,G,H 9 S,A,B,C,D,E,F,G,H 10 S,A,B,C,D,E,F,G,H 50 S BA E FDC G H
  • 51. BFS: EXAMPLE Q Visited 1 S S 2 A,B S,A,B 3 B,C,D S,A,B,C,D 4 C,D,E,F S,A,B,C,D,E,F 5 D,E,F,G,H S,A,B,C,D,E,F,G,H 6 E,F,G,H S,A,B,C,D,E,F,G,H 7 F,G,H S,A,B,C,D,E,F,G,H 8 G,H S,A,B,C,D,E,F,G,H 9 H S,A,B,C,D,E,F,G,H 10 S,A,B,C,D,E,F,G,H 51 S BA E FDC G H
  • 52. THANKS • Implement DFS and BFS in lab and submit softcopy at m.hammad.wasim@gmail.com 52