SlideShare une entreprise Scribd logo
1  sur  58
LEARNING OBJECTIVES
 After completing this chapter, you will be able to:
1. Represent the graph using array and Linked list
2. Traverse the graph
3. Calculate minimum cost spanning tree
4. Calculate the shortest route from source to all
other nodes
GRAPHS
 Graph is a collection of nodes or vertices connected
together through edges or arcs.
 It is representation of a set of objects where some
pairs of objects are connected by links. The
interconnected objects are represented by points
termed as vertices, and the links that connect the
vertices are called edges.
 Formally, a graph is a pair of sets V, E, where V is the
set of vertices and E is the set of edges, connecting
the pairs of vertices.
 Take a look at the following graph −
CONT.…
In the above graph,
V = {a, b, c, d, e}
E = {ab, ac, bd, cd, de}
USES OF GRAPHS
 Graphs are used to model electrical circuits,
chemical compounds, highway maps, and so on.
 They are also used in the analysis of electrical
circuits, finding the shortest route, project
planning, linguistics, genetics, social science, and
so forth.
GRAPH DEFINITIONS AND NOTATIONS
 A graph G is a pair, G = (V, E),
 where V is a finite nonempty set, called the set of vertices of
G. E is called the set of edges.
 Let V(G) denote the set of vertices, and E(G) denote the set
of edges of a graph G.
 If the elements of E(G) are ordered pairs, G is called a
directed graph or digraph;,
 otherwise, G is called an undirected graph. In an
undirected graph, the pairs (u, v) and (v, u) represent the
same edge.
CONT.…
 Let G be a graph.
 A graph H is called a sub-graph of G if V(H) ⊆
V(G) and E(H) ⊆ ^E(G);
 that is, every vertex of H is a vertex of G, and
every edge in H is an edge in G.
CONT.….
 A graph can be shown pictorially.
 The vertices are drawn as circles, and a label
inside the circle represents the vertex.
1. In an undirected graph, the edges are drawn using
lines.
2. In a directed graph, the edges are drawn using
arrows.
CONT.….
GRAPH DATA STRUCTURE
 Mathematical graphs can be represented in data-
structure.
 We can represent a graph using an array of
vertices and a two dimensional array of edges
IMPORTANT GRAPH’S TERMS
 Vertex − Each node of the graph is represented as a
vertex. In example given below, labeled circle represents
vertices.
 So A to E are vertices. We can represent them using an array
as shown in image below. Here A can be identified by index
0. B can be identified using index 1 and so on.
 Edge − Edge represents a path between two vertices or a
line between two vertices.
 In example given below, lines from A to B, B to D and
so on represents edges..
CONT.…..
 Adjacency − Two node or vertices are adjacent if they
are connected to each other through an edge. In
example given below, B is adjacent to A, D is adjacent to
B and so on.
 Path − Path represents a sequence of edges between
two vertices. In example given below, ABDE represents a
path from A to E.
GRAPH REPRESENTATION
 A graph can be represented in several
ways.
 Two common ways:
1. adjacency matrices and
2. adjacency lists.
ADJACENCY MATRIX
 Let G be a graph with n vertices, where n > 0.
 Let V(G) = {v1, v2, ..., vn}.
 The adjacency matrix AG is a two dimensional matrix n x
^n matrix such that the (i, j)th entry of AG is 1 if there is
an edge from vi to vj;
 otherwise, the (i,j)th entry is zero.
ADJACENCY MATRIX FOR GRAPHS
Adjacency Matrix for graphs (a) and (b)
ADJACENCY LISTS
 An Adjacency List is a linked list of Vertices adjacent to a given
vertex
 Let G be a graph with n vertices, where n > 0.
 Let V(G) = {v1, v2, ..., vn}.
 In the adjacency list representation, corresponding to each vertex,
v, there is a linked list such that each node of the linked list
contains the vertex u, such that (v, u) ⊆ E(G).
 Because there are n nodes, we use an array, A, of size n, such that
A[i] is a reference variable pointing to the first node of the linked
list containing the vertices to which vi is adjacent. Each node has
two components, say vertex and link.
 The component vertex contains the index of the vertex adjacent to
vertex i
ADJACENCY LIST OF GRAPH IN EXAMPLE
OPERATIONS ON GRAPHS
 Following are the basic primary operations of a Graph
1. Add Vertex − add a vertex to a graph.
2. Add Edge − add an edge between two vertices of a graph.
3. Display Vertex − display a vertex of a graph.
4. Create the graph. That is, store the graph in computer
memory using a particular graph representation.
5. Clear the graph. This operation makes the graph empty.
6. Determine whether the graph is empty.
7. Traverse the graph.
GRAPH TRAVERSALS
 Processing a graph requires the ability to traverse the
graph. Traversing a graph is similar to traversing a binary
tree, except that traversing a graph is a bit more
complicated. Recall that a binary tree has no cycles.
 Also, starting at the root node, we can traverse the entire
tree.
 On the other hand, a graph might have cycles and we
might not be able to traverse the entire graph from a single
vertex (for example, if the graph is not connected).
 Therefore, we must keep track of the vertices that have
been visited. We must also traverse the graph from each
vertex (that has not been visited) of the graph.
CONT.….
 This ensures that the entire graph is traversed.
 The two most common graph traversal
algorithms are:
1. Depth first traversal
2. Breadth first traversal
DEPTH FIRST TRAVERSAL
 Depth First Search algorithm DFS traverses a graph in a depth
ward motion and uses a stack to remember to get the next
vertex to start a search when a dead end occurs in any
iteration.
 Depth First Search- a search that begins by visiting vertex V,
an then recursively searches the unvisited vertices adjacent to V.
 The depth first traversal is similar to the preorder traversal of a
binary tree.
 An initial or source vertex is identified to start traversing, then
from that vertex any one vertex which is adjacent to the current
vertex is traversed i.e. only one adjacent vertex is traversed
from the vertex which had been traversed last.
CONT.….
DEPTH FIRST TRAVERSAL
As in example given above, DFS algorithm traverses from A to B
to C to D first then to E, then to F and lastly to G.
CONT…
 It employs following rules.
 Rule 1 − Visit adjacent unvisited vertex. Mark it visited.
Display it. Push it in a stack.
 Rule 2 − If no adjacent vertex found, pop up a vertex
from stack.
 Rule 3 − Repeat Rule 1 and Rule 2 until stack is empty.
CONT.….
CONT….
CONT.….
CONT.….
CONT.….
CONT.….
CONT.…..
As C does not have any unvisited adjacent node so we keep popping the
stack until we find a node which has unvisited adjacent node. In this
case, there's none and we keep popping until stack is empty.
BREADTH FIRST TRAVERSAL
 Breadth First Search algorithm BFS traverses a graph in a
breadth wards motion and uses a queue to remember to
get the next vertex to start a search when a dead end
occurs in any iteration.
 Breadth-First search- A search that begins by visiting
vertex V, then visits the vertices adjacent to V, then visit the
vertices adjacent to each of V’s adjacent vertices, and so
on.
 The breadth first traversal of a graph is similar to traversing
a binary tree level by level (the nodes at each level are
visited from left to right).All the nodes at any level, i, are
visited before visiting the nodes at level i + 1.
CONT….
CONT.…
As in example given above, BFS algorithm traverses from A to B to E
to F first then to C and G lastly to D.
CONT…
 It employs following rules.
 Rule 1 − Visit adjacent unvisited vertex. Mark it visited.
Display it. Insert it in a queue.
 Rule 2 − If no adjacent vertex found, remove the first
vertex from queue.
 Rule 3 − Repeat Rule 1 and Rule 2 until queue is empty.
CONT.….
CONT.….
CONT…
CONT.…
CONT.…
CONT.…..
CONT.…..
At this stage we are left with no unmarked unvisited nodes. But as per
algorithm we keep on dequeuing in order to get all unvisited nodes.
When the queue gets emptied the program is over.
SHORTEST PATH ALGORITHM
 Shortest path can be calculated only for the weighted
graphs.
 The edges connecting two vertices can be assigned a
nonnegative real number, called the weight of the edge.
 A graph with such weighted edges is called a weighted
graph.
 Let G be a weighted graph. Let u and v be two vertices in
G, and let P be a path in G from u to v.
 The weight of the path P is the sum of the weights of all
the edges on the path P, which is also called the weight of
v from u via P.
CONT.….
 Let G be a weighted graph representing a highway
structure. Suppose that the weight of an edge represents
the travel time.
 For example, to plan monthly business trips, a
salesperson wants to find the shortest path (that is, the
path with the smallest weight) from her or his city to
every other city in the graph. Many such problems exist in
which we want to find the shortest path from a given
vertex, called the source, to every other vertex in the
graph.
 This section describes the shortest path algorithm, also
called the greedy algorithm, developed by Dijkstra.
SHORTEST PATH
 Given a vertex, say vertex (that is, a source), this section
describes the shortest path algorithm.
 The general algorithm is:
1. Initialize the array smallest Weight so that smallest
Weight[u]=weights[vertex, u].
2. Set smallest Weight[vertex] = 0.
3. Find the vertex, v, that is closest to vertex for which the shortest path has
not been determined.
4. Mark v as the (next) vertex for which the smallest weight is found.
5. For each vertex w in G, such that the shortest path from vertex to w has
not been determined and an edge (v, w) exists, if the weight of the path
to w via v is smaller than its current weight, update the weight of w to the
weight of v + the weight of the edge (v, w).
CONT…..
CONT.….
CONT……
 Therefore A-B-D (3) < A-D (5)
 Adjusted from B
 Select A-C
MINIMUM SPANNING TREES
 A spanning tree of a graph, G, is a set of |V|-1 edges that connect all vertices
of the graph.
 Suppose we have a group of islands that we wish to link with bridges so that
it is possible to travel from one island to any other in the group.
 Further suppose that (as usual) our government wishes to spend the
absolute minimum amount on this project (because other factors like the
cost of using, maintaining, etc., these bridges will probably be the
responsibility of some future government). The engineers are able to
produce a cost for a bridge linking each possible pair of islands. The set of
bridges which will enable one to travel from any island to any other at
minimum capital cost to the government is the minimum spanning tree.
 In general, it is possible to construct multiple spanning trees for a graph, G.
If a cost, cij, is associated with each edge, eij = (vi,vj), then the minimum
spanning tree is the set of edges, E span, forming a spanning tree, such that:
 C = sum( cij | all eij in Espan ) is a minimum.
KRUSKAL'S ALGORITHM
 This algorithm creates a forest of trees. Initially the
forest consists of n single node trees (and no edges). At
each step, we add one (the cheapest one) edge so that
it joins two trees together.
 If it were to form a cycle, it would simply link two
nodes that were already part of a single connected tree,
so that this edge would not be needed.
CONT.….
CONT.…
 The following sequence of diagrams illustrates Kruskal's algorithm
in operation.
 Kruskal’s Algorithm
CONT.….
CONT.….
CONT.…
CONT.….
SUMMARY
 Graph is a collection of nodes Connected together using
edges.
 Graph can be traversed using DFS or BFS
 Shortest path for a vertex with other vertices can be
calculated using Dijkstra’s algorithm.
 Spanning tree is an acyclic graph.
 Minimum cost spanning trees can be derived using
Kruskal’s algorithm
TEST YOUR UNDERSTANDING
logic.pptx

Contenu connexe

Similaire à logic.pptx

Graph_data_structure_information_engineering.pptx
Graph_data_structure_information_engineering.pptxGraph_data_structure_information_engineering.pptx
Graph_data_structure_information_engineering.pptxsahilpawar2426
 
Slides Chapter10.1 10.2
Slides Chapter10.1 10.2Slides Chapter10.1 10.2
Slides Chapter10.1 10.2showslidedump
 
Graph terminology and algorithm and tree.pptx
Graph terminology and algorithm and tree.pptxGraph terminology and algorithm and tree.pptx
Graph terminology and algorithm and tree.pptxasimshahzad8611
 
VANU no sql ppt.pptx
VANU no sql ppt.pptxVANU no sql ppt.pptx
VANU no sql ppt.pptxMJeyavarthini
 
data structures and algorithms Unit 2
data structures and algorithms Unit 2data structures and algorithms Unit 2
data structures and algorithms Unit 2infanciaj
 
Graph theory concepts complex networks presents-rouhollah nabati
Graph theory concepts   complex networks presents-rouhollah nabatiGraph theory concepts   complex networks presents-rouhollah nabati
Graph theory concepts complex networks presents-rouhollah nabatinabati
 
Graph Theory 117 © David Lippman Creative Commons BY-
  Graph Theory   117 © David Lippman  Creative Commons BY-  Graph Theory   117 © David Lippman  Creative Commons BY-
Graph Theory 117 © David Lippman Creative Commons BY-maple8qvlisbey
 
Graph Theory 117 © David Lippman Creative Commons BY-.docx
Graph Theory   117 © David Lippman  Creative Commons BY-.docxGraph Theory   117 © David Lippman  Creative Commons BY-.docx
Graph Theory 117 © David Lippman Creative Commons BY-.docxdurantheseldine
 
Graph Theory,Graph Terminologies,Planar Graph & Graph Colouring
Graph Theory,Graph Terminologies,Planar Graph & Graph ColouringGraph Theory,Graph Terminologies,Planar Graph & Graph Colouring
Graph Theory,Graph Terminologies,Planar Graph & Graph ColouringSaurabh Kaushik
 
Mithfh lecturenotes 9
Mithfh lecturenotes 9Mithfh lecturenotes 9
Mithfh lecturenotes 9Praveen Kumar
 
Graph ASS DBATU.pptx
Graph ASS DBATU.pptxGraph ASS DBATU.pptx
Graph ASS DBATU.pptxARVIND SARDAR
 
graph representation.pdf
graph representation.pdfgraph representation.pdf
graph representation.pdfamitbhachne
 

Similaire à logic.pptx (20)

UNIT III.pptx
UNIT III.pptxUNIT III.pptx
UNIT III.pptx
 
Data structure note
Data structure noteData structure note
Data structure note
 
Graph_data_structure_information_engineering.pptx
Graph_data_structure_information_engineering.pptxGraph_data_structure_information_engineering.pptx
Graph_data_structure_information_engineering.pptx
 
Slides Chapter10.1 10.2
Slides Chapter10.1 10.2Slides Chapter10.1 10.2
Slides Chapter10.1 10.2
 
Data structure and algorithm
Data structure and algorithmData structure and algorithm
Data structure and algorithm
 
Graph terminology and algorithm and tree.pptx
Graph terminology and algorithm and tree.pptxGraph terminology and algorithm and tree.pptx
Graph terminology and algorithm and tree.pptx
 
Vanmathy no sql
Vanmathy no sql Vanmathy no sql
Vanmathy no sql
 
VANU no sql ppt.pptx
VANU no sql ppt.pptxVANU no sql ppt.pptx
VANU no sql ppt.pptx
 
Spanningtreesppt
SpanningtreespptSpanningtreesppt
Spanningtreesppt
 
Graph
GraphGraph
Graph
 
data structures and algorithms Unit 2
data structures and algorithms Unit 2data structures and algorithms Unit 2
data structures and algorithms Unit 2
 
Graph theory concepts complex networks presents-rouhollah nabati
Graph theory concepts   complex networks presents-rouhollah nabatiGraph theory concepts   complex networks presents-rouhollah nabati
Graph theory concepts complex networks presents-rouhollah nabati
 
Graph Theory 117 © David Lippman Creative Commons BY-
  Graph Theory   117 © David Lippman  Creative Commons BY-  Graph Theory   117 © David Lippman  Creative Commons BY-
Graph Theory 117 © David Lippman Creative Commons BY-
 
Graph Theory 117 © David Lippman Creative Commons BY-.docx
Graph Theory   117 © David Lippman  Creative Commons BY-.docxGraph Theory   117 © David Lippman  Creative Commons BY-.docx
Graph Theory 117 © David Lippman Creative Commons BY-.docx
 
Graph Theory,Graph Terminologies,Planar Graph & Graph Colouring
Graph Theory,Graph Terminologies,Planar Graph & Graph ColouringGraph Theory,Graph Terminologies,Planar Graph & Graph Colouring
Graph Theory,Graph Terminologies,Planar Graph & Graph Colouring
 
graph ASS (1).ppt
graph ASS (1).pptgraph ASS (1).ppt
graph ASS (1).ppt
 
Graphs.pptx
Graphs.pptxGraphs.pptx
Graphs.pptx
 
Mithfh lecturenotes 9
Mithfh lecturenotes 9Mithfh lecturenotes 9
Mithfh lecturenotes 9
 
Graph ASS DBATU.pptx
Graph ASS DBATU.pptxGraph ASS DBATU.pptx
Graph ASS DBATU.pptx
 
graph representation.pdf
graph representation.pdfgraph representation.pdf
graph representation.pdf
 

Dernier

Q-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITWQ-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITWQuiz Club NITW
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)lakshayb543
 
week 1 cookery 8 fourth - quarter .pptx
week 1 cookery 8  fourth  -  quarter .pptxweek 1 cookery 8  fourth  -  quarter .pptx
week 1 cookery 8 fourth - quarter .pptxJonalynLegaspi2
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxHumphrey A Beña
 
Unraveling Hypertext_ Analyzing Postmodern Elements in Literature.pptx
Unraveling Hypertext_ Analyzing  Postmodern Elements in  Literature.pptxUnraveling Hypertext_ Analyzing  Postmodern Elements in  Literature.pptx
Unraveling Hypertext_ Analyzing Postmodern Elements in Literature.pptxDhatriParmar
 
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...Association for Project Management
 
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptxDIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptxMichelleTuguinay1
 
Multi Domain Alias In the Odoo 17 ERP Module
Multi Domain Alias In the Odoo 17 ERP ModuleMulti Domain Alias In the Odoo 17 ERP Module
Multi Domain Alias In the Odoo 17 ERP ModuleCeline George
 
How to Make a Duplicate of Your Odoo 17 Database
How to Make a Duplicate of Your Odoo 17 DatabaseHow to Make a Duplicate of Your Odoo 17 Database
How to Make a Duplicate of Your Odoo 17 DatabaseCeline George
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfPatidar M
 
How to Fix XML SyntaxError in Odoo the 17
How to Fix XML SyntaxError in Odoo the 17How to Fix XML SyntaxError in Odoo the 17
How to Fix XML SyntaxError in Odoo the 17Celine George
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptxmary850239
 
4.11.24 Mass Incarceration and the New Jim Crow.pptx
4.11.24 Mass Incarceration and the New Jim Crow.pptx4.11.24 Mass Incarceration and the New Jim Crow.pptx
4.11.24 Mass Incarceration and the New Jim Crow.pptxmary850239
 
MS4 level being good citizen -imperative- (1) (1).pdf
MS4 level   being good citizen -imperative- (1) (1).pdfMS4 level   being good citizen -imperative- (1) (1).pdf
MS4 level being good citizen -imperative- (1) (1).pdfMr Bounab Samir
 
Scientific Writing :Research Discourse
Scientific  Writing :Research  DiscourseScientific  Writing :Research  Discourse
Scientific Writing :Research DiscourseAnita GoswamiGiri
 
Measures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped dataMeasures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped dataBabyAnnMotar
 
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...DhatriParmar
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management systemChristalin Nelson
 

Dernier (20)

Q-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITWQ-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITW
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
 
week 1 cookery 8 fourth - quarter .pptx
week 1 cookery 8  fourth  -  quarter .pptxweek 1 cookery 8  fourth  -  quarter .pptx
week 1 cookery 8 fourth - quarter .pptx
 
Paradigm shift in nursing research by RS MEHTA
Paradigm shift in nursing research by RS MEHTAParadigm shift in nursing research by RS MEHTA
Paradigm shift in nursing research by RS MEHTA
 
Faculty Profile prashantha K EEE dept Sri Sairam college of Engineering
Faculty Profile prashantha K EEE dept Sri Sairam college of EngineeringFaculty Profile prashantha K EEE dept Sri Sairam college of Engineering
Faculty Profile prashantha K EEE dept Sri Sairam college of Engineering
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
 
Unraveling Hypertext_ Analyzing Postmodern Elements in Literature.pptx
Unraveling Hypertext_ Analyzing  Postmodern Elements in  Literature.pptxUnraveling Hypertext_ Analyzing  Postmodern Elements in  Literature.pptx
Unraveling Hypertext_ Analyzing Postmodern Elements in Literature.pptx
 
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
 
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptxDIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
 
Multi Domain Alias In the Odoo 17 ERP Module
Multi Domain Alias In the Odoo 17 ERP ModuleMulti Domain Alias In the Odoo 17 ERP Module
Multi Domain Alias In the Odoo 17 ERP Module
 
How to Make a Duplicate of Your Odoo 17 Database
How to Make a Duplicate of Your Odoo 17 DatabaseHow to Make a Duplicate of Your Odoo 17 Database
How to Make a Duplicate of Your Odoo 17 Database
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdf
 
How to Fix XML SyntaxError in Odoo the 17
How to Fix XML SyntaxError in Odoo the 17How to Fix XML SyntaxError in Odoo the 17
How to Fix XML SyntaxError in Odoo the 17
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx
 
4.11.24 Mass Incarceration and the New Jim Crow.pptx
4.11.24 Mass Incarceration and the New Jim Crow.pptx4.11.24 Mass Incarceration and the New Jim Crow.pptx
4.11.24 Mass Incarceration and the New Jim Crow.pptx
 
MS4 level being good citizen -imperative- (1) (1).pdf
MS4 level   being good citizen -imperative- (1) (1).pdfMS4 level   being good citizen -imperative- (1) (1).pdf
MS4 level being good citizen -imperative- (1) (1).pdf
 
Scientific Writing :Research Discourse
Scientific  Writing :Research  DiscourseScientific  Writing :Research  Discourse
Scientific Writing :Research Discourse
 
Measures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped dataMeasures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped data
 
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management system
 

logic.pptx

  • 1. LEARNING OBJECTIVES  After completing this chapter, you will be able to: 1. Represent the graph using array and Linked list 2. Traverse the graph 3. Calculate minimum cost spanning tree 4. Calculate the shortest route from source to all other nodes
  • 2. GRAPHS  Graph is a collection of nodes or vertices connected together through edges or arcs.  It is representation of a set of objects where some pairs of objects are connected by links. The interconnected objects are represented by points termed as vertices, and the links that connect the vertices are called edges.  Formally, a graph is a pair of sets V, E, where V is the set of vertices and E is the set of edges, connecting the pairs of vertices.  Take a look at the following graph −
  • 3. CONT.… In the above graph, V = {a, b, c, d, e} E = {ab, ac, bd, cd, de}
  • 4. USES OF GRAPHS  Graphs are used to model electrical circuits, chemical compounds, highway maps, and so on.  They are also used in the analysis of electrical circuits, finding the shortest route, project planning, linguistics, genetics, social science, and so forth.
  • 5. GRAPH DEFINITIONS AND NOTATIONS  A graph G is a pair, G = (V, E),  where V is a finite nonempty set, called the set of vertices of G. E is called the set of edges.  Let V(G) denote the set of vertices, and E(G) denote the set of edges of a graph G.  If the elements of E(G) are ordered pairs, G is called a directed graph or digraph;,  otherwise, G is called an undirected graph. In an undirected graph, the pairs (u, v) and (v, u) represent the same edge.
  • 6. CONT.…  Let G be a graph.  A graph H is called a sub-graph of G if V(H) ⊆ V(G) and E(H) ⊆ ^E(G);  that is, every vertex of H is a vertex of G, and every edge in H is an edge in G.
  • 7. CONT.….  A graph can be shown pictorially.  The vertices are drawn as circles, and a label inside the circle represents the vertex. 1. In an undirected graph, the edges are drawn using lines. 2. In a directed graph, the edges are drawn using arrows.
  • 9. GRAPH DATA STRUCTURE  Mathematical graphs can be represented in data- structure.  We can represent a graph using an array of vertices and a two dimensional array of edges
  • 10. IMPORTANT GRAPH’S TERMS  Vertex − Each node of the graph is represented as a vertex. In example given below, labeled circle represents vertices.  So A to E are vertices. We can represent them using an array as shown in image below. Here A can be identified by index 0. B can be identified using index 1 and so on.  Edge − Edge represents a path between two vertices or a line between two vertices.  In example given below, lines from A to B, B to D and so on represents edges..
  • 11. CONT.…..  Adjacency − Two node or vertices are adjacent if they are connected to each other through an edge. In example given below, B is adjacent to A, D is adjacent to B and so on.  Path − Path represents a sequence of edges between two vertices. In example given below, ABDE represents a path from A to E.
  • 12. GRAPH REPRESENTATION  A graph can be represented in several ways.  Two common ways: 1. adjacency matrices and 2. adjacency lists.
  • 13. ADJACENCY MATRIX  Let G be a graph with n vertices, where n > 0.  Let V(G) = {v1, v2, ..., vn}.  The adjacency matrix AG is a two dimensional matrix n x ^n matrix such that the (i, j)th entry of AG is 1 if there is an edge from vi to vj;  otherwise, the (i,j)th entry is zero.
  • 14. ADJACENCY MATRIX FOR GRAPHS Adjacency Matrix for graphs (a) and (b)
  • 15. ADJACENCY LISTS  An Adjacency List is a linked list of Vertices adjacent to a given vertex  Let G be a graph with n vertices, where n > 0.  Let V(G) = {v1, v2, ..., vn}.  In the adjacency list representation, corresponding to each vertex, v, there is a linked list such that each node of the linked list contains the vertex u, such that (v, u) ⊆ E(G).  Because there are n nodes, we use an array, A, of size n, such that A[i] is a reference variable pointing to the first node of the linked list containing the vertices to which vi is adjacent. Each node has two components, say vertex and link.  The component vertex contains the index of the vertex adjacent to vertex i
  • 16. ADJACENCY LIST OF GRAPH IN EXAMPLE
  • 17. OPERATIONS ON GRAPHS  Following are the basic primary operations of a Graph 1. Add Vertex − add a vertex to a graph. 2. Add Edge − add an edge between two vertices of a graph. 3. Display Vertex − display a vertex of a graph. 4. Create the graph. That is, store the graph in computer memory using a particular graph representation. 5. Clear the graph. This operation makes the graph empty. 6. Determine whether the graph is empty. 7. Traverse the graph.
  • 18. GRAPH TRAVERSALS  Processing a graph requires the ability to traverse the graph. Traversing a graph is similar to traversing a binary tree, except that traversing a graph is a bit more complicated. Recall that a binary tree has no cycles.  Also, starting at the root node, we can traverse the entire tree.  On the other hand, a graph might have cycles and we might not be able to traverse the entire graph from a single vertex (for example, if the graph is not connected).  Therefore, we must keep track of the vertices that have been visited. We must also traverse the graph from each vertex (that has not been visited) of the graph.
  • 19. CONT.….  This ensures that the entire graph is traversed.  The two most common graph traversal algorithms are: 1. Depth first traversal 2. Breadth first traversal
  • 20. DEPTH FIRST TRAVERSAL  Depth First Search algorithm DFS traverses a graph in a depth ward motion and uses a stack to remember to get the next vertex to start a search when a dead end occurs in any iteration.  Depth First Search- a search that begins by visiting vertex V, an then recursively searches the unvisited vertices adjacent to V.  The depth first traversal is similar to the preorder traversal of a binary tree.  An initial or source vertex is identified to start traversing, then from that vertex any one vertex which is adjacent to the current vertex is traversed i.e. only one adjacent vertex is traversed from the vertex which had been traversed last.
  • 22. DEPTH FIRST TRAVERSAL As in example given above, DFS algorithm traverses from A to B to C to D first then to E, then to F and lastly to G.
  • 23. CONT…  It employs following rules.  Rule 1 − Visit adjacent unvisited vertex. Mark it visited. Display it. Push it in a stack.  Rule 2 − If no adjacent vertex found, pop up a vertex from stack.  Rule 3 − Repeat Rule 1 and Rule 2 until stack is empty.
  • 30. CONT.….. As C does not have any unvisited adjacent node so we keep popping the stack until we find a node which has unvisited adjacent node. In this case, there's none and we keep popping until stack is empty.
  • 31. BREADTH FIRST TRAVERSAL  Breadth First Search algorithm BFS traverses a graph in a breadth wards motion and uses a queue to remember to get the next vertex to start a search when a dead end occurs in any iteration.  Breadth-First search- A search that begins by visiting vertex V, then visits the vertices adjacent to V, then visit the vertices adjacent to each of V’s adjacent vertices, and so on.  The breadth first traversal of a graph is similar to traversing a binary tree level by level (the nodes at each level are visited from left to right).All the nodes at any level, i, are visited before visiting the nodes at level i + 1.
  • 33. CONT.… As in example given above, BFS algorithm traverses from A to B to E to F first then to C and G lastly to D.
  • 34. CONT…  It employs following rules.  Rule 1 − Visit adjacent unvisited vertex. Mark it visited. Display it. Insert it in a queue.  Rule 2 − If no adjacent vertex found, remove the first vertex from queue.  Rule 3 − Repeat Rule 1 and Rule 2 until queue is empty.
  • 41. CONT.….. At this stage we are left with no unmarked unvisited nodes. But as per algorithm we keep on dequeuing in order to get all unvisited nodes. When the queue gets emptied the program is over.
  • 42. SHORTEST PATH ALGORITHM  Shortest path can be calculated only for the weighted graphs.  The edges connecting two vertices can be assigned a nonnegative real number, called the weight of the edge.  A graph with such weighted edges is called a weighted graph.  Let G be a weighted graph. Let u and v be two vertices in G, and let P be a path in G from u to v.  The weight of the path P is the sum of the weights of all the edges on the path P, which is also called the weight of v from u via P.
  • 43. CONT.….  Let G be a weighted graph representing a highway structure. Suppose that the weight of an edge represents the travel time.  For example, to plan monthly business trips, a salesperson wants to find the shortest path (that is, the path with the smallest weight) from her or his city to every other city in the graph. Many such problems exist in which we want to find the shortest path from a given vertex, called the source, to every other vertex in the graph.  This section describes the shortest path algorithm, also called the greedy algorithm, developed by Dijkstra.
  • 44. SHORTEST PATH  Given a vertex, say vertex (that is, a source), this section describes the shortest path algorithm.  The general algorithm is: 1. Initialize the array smallest Weight so that smallest Weight[u]=weights[vertex, u]. 2. Set smallest Weight[vertex] = 0. 3. Find the vertex, v, that is closest to vertex for which the shortest path has not been determined. 4. Mark v as the (next) vertex for which the smallest weight is found. 5. For each vertex w in G, such that the shortest path from vertex to w has not been determined and an edge (v, w) exists, if the weight of the path to w via v is smaller than its current weight, update the weight of w to the weight of v + the weight of the edge (v, w).
  • 47. CONT……  Therefore A-B-D (3) < A-D (5)  Adjusted from B  Select A-C
  • 48. MINIMUM SPANNING TREES  A spanning tree of a graph, G, is a set of |V|-1 edges that connect all vertices of the graph.  Suppose we have a group of islands that we wish to link with bridges so that it is possible to travel from one island to any other in the group.  Further suppose that (as usual) our government wishes to spend the absolute minimum amount on this project (because other factors like the cost of using, maintaining, etc., these bridges will probably be the responsibility of some future government). The engineers are able to produce a cost for a bridge linking each possible pair of islands. The set of bridges which will enable one to travel from any island to any other at minimum capital cost to the government is the minimum spanning tree.  In general, it is possible to construct multiple spanning trees for a graph, G. If a cost, cij, is associated with each edge, eij = (vi,vj), then the minimum spanning tree is the set of edges, E span, forming a spanning tree, such that:  C = sum( cij | all eij in Espan ) is a minimum.
  • 49. KRUSKAL'S ALGORITHM  This algorithm creates a forest of trees. Initially the forest consists of n single node trees (and no edges). At each step, we add one (the cheapest one) edge so that it joins two trees together.  If it were to form a cycle, it would simply link two nodes that were already part of a single connected tree, so that this edge would not be needed.
  • 51. CONT.…  The following sequence of diagrams illustrates Kruskal's algorithm in operation.  Kruskal’s Algorithm
  • 56. SUMMARY  Graph is a collection of nodes Connected together using edges.  Graph can be traversed using DFS or BFS  Shortest path for a vertex with other vertices can be calculated using Dijkstra’s algorithm.  Spanning tree is an acyclic graph.  Minimum cost spanning trees can be derived using Kruskal’s algorithm