Graph in data structure

Pooja Bhojwani
Pooja BhojwaniPooja Bhojwani
Graph in data structure
Graph in data structure
Graph in data structure
Graph in data structure
A graph G = (V,E) is
composed of:
• V: set of vertices(or points
or nodes)
• E: set of edges connecting
the vertices in v or
(A set E of edges
such that each edge e in E is
identified with a unique
(unordered) pair [u, v] of
nodes in V, denotes by e =
[u, v])
*
example=
V= {a,b,c,d,e}
E= {(a,b),(a,c),(a,d),
(b,e),(c,d),(c,e), (d,e)}
Graphs cont..
 If the element are in ordered pairs it is call directed graph.
 If the pairs represent the same edge it is known as undirected graph.
 Subgraph happened when every vertex include in other vertex and
every edge is include in other edge.
1 2
3 4
5
1 2
3 4
5
Some of the applications of graphs are :
• Mazes (using stacks)
• Networks (computer, cities ....)
• Maps (any geographic databases )
• Graphics : Geometrical Objects
• Neighborhood graphs
• Voronoi diagrams
Many geometrical objects such as cubes, polyhedral, and wire
frame car models, may be thought of as graphs. These graphs
are more than just nodes and edges. Their geometrical
structure must also be taken into account. For example
consider the cube:
• This structure contains three kinds of objects
• vertices
• edges
• faces
Edges are crucial since, in a three dimensional object, an edge
will always belong to only two faces and two nodes. For this
reason it makes sense to number the edges. Faces become
linked list of edges, and each edge lives in only two faces
Graph in data structure
 Adjacency Matrix Representation
 An adjacency matrix is one of the two common ways to represent a
graph.
 The adjacency matrix shows which nodes are adjacent to one another.
Two nodes are adjacent if there is an edge connecting them.
 In the case of a directed graph, if node j is adjacent to node i, there is
an edge from i to j. In other words, if j is adjacent to i, you can get from
i to j by traversing one edge.
 For a given graph with n nodes, the adjacency matrix will have
dimensions of n x n.
 For an unweighted graph, the adjacency matrix will be populated with
boolean values.
• For any given node i, you can determine its adjacent nodes
by looking at row of the adjacency matrix. A value of true at
indicates that there is an edge from node i to node j, and
false indicating no edge. In an undirected graph, the values
of and will be equal.
• In a weighted graph, the boolean values will be replaced by
the weight of the edge connecting the two nodes, with a
special value that indicates the absence of an edge.
• The memory use of an adjacency matrix is O(n2).
Adjacency Matrix
A B C D E
A 0 0 0 1 0
B 0 0 0 1 0
C 0 1 0 0 0
D 0 0 0 0 1
E 0 0 1 0 0
Adjacency List Representation
• One way to have the graph maintain a list of lists, in
which the first list is a list of indices corresponding to
each node in the graph.
• Each of these refer to another list that stores a the
index of each adjacent node to this one. It also useful to
associate the weight of each link with the adjacent node
in this list.
Example: An undirected graph contains four nodes 1, 2, 3
and 4. 1 is linked to 2 and 3. 2 is linked to 3. 3 is linked to
4.
1 - [2, 3]
2 - [1, 3]
3 - [1, 2, 4]
4 - [3]
Adjacency List Representation
• It useful to store the list of all the nodes in the
graph in a hash table. The keys then would
correspond to the indices of each node and the
value would be a reference to the list of adjacent
node indecies.
• Another implementation might require that each
node keep a list of its adjacent nodes.
Graph in data structure
Graph Traversal
•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)
• Start at vertex v, visit its neighbor w, then w's neighbor y and
keep going until reach 'a dead end' then iterate back and visit
nodes reachable from second last visited vertex and keep
applying the same principle.
• General algorithm:
for each vertex v in the graph
if v is not visited
start the depth first traversal at v
17
0 1 2 4
5
6
3
7
8
9
10
Directed graph G3
The depth first ordering of the vertices graph G3:
0 1 2 3 4 5 6 8 10 7 9
The general algorithm to do a depth first traversal at a
given
node v is:
1. Mark node v as visited
2. Visit the node
3. For each vertex u adjacent to v
if u is not visited
start the depth first traversal at u
Breadth-First Search (Queue)
Breadth first search visits the nodes neighbours and then the
univisited neighbours of the neighbours etc. If it starts on vertex a
it will go to all vertices that have an edge from a. If some points are
not reachable it will have to start another BFS from a new vertex.
Base on the same example in Depth-First Traversal :
0 1 2 3 4 5 6 8 10 7 9
Start the traversal at vertex 0. After visiting vertex 0 next visit the
vertices that are directly connected to it, that are 1 and 5. Next visit
the vertices that are directed connected to 1 and are not visit, that
is 2 and 3. Then visit vertices that directly connected to 5 and are
not visited, that is 6. Then continue to next path.
Breadth-First Search
The general algorithm:
a. for each vertex v in the graph
if v is not visited
add v to the queue // start the breadth first search at v
b. Mark v as visited
c. While the queue is not empty
c.1. Remove vertex u from the queue
c.2. Retrieve the vertices adjacent to u
c.3. for each vertex w that is adjacent to u
if w is not visited
c.3.1. Add w to the queue
c.3.2. Mark w as visited
• We may also want to associate some cost or weight to the traversal of an
edge. When we add this information, the graph is called weighted. An
example of a weighted graph would be the distance between the capitals of a
set of countries.
• Directed and undirected graphs may both be weighted. The operations on a
weighted graph are the same with addition of a weight parameter during edge
creation:
• Weighted Graph Operations (an extension of undirected/directed graph
operations)
• make-edge(vertex u, vertex v, weight w): edge
• Create an edge between u and v with weight w. In a directed graph, the edge
will flow from u to v.
• Weight graph also being used to find the shortest path.
Graph in data structure
Shortest Path
Shortest path A-B
is 2
Shortest path from
D to A is (DA) = 1
Shortest path from C
to A is (CBA) = 3
Shortest path from E to A is
(EDA) = 4
Shortest path from F to A is
(FCBA) = 5
Minimum Spanning Tree
25
A spanning tree of a graph is
just a subgraph that contains
all the vertices and is a tree.
A graph may have many
spanning trees; for instance
the complete graph on four
vertices
fom the figure of graph k4 ..it
has sixteen spanning trees:
26
Graph in data structure
Graph in data structure
1 de 28

Recomendados

Data Structures - Lecture 10 [Graphs] por
Data Structures - Lecture 10 [Graphs]Data Structures - Lecture 10 [Graphs]
Data Structures - Lecture 10 [Graphs]Muhammad Hammad Waseem
6.4K vistas52 diapositivas
Graph Data Structure por
Graph Data StructureGraph Data Structure
Graph Data StructureKeno benti
616 vistas41 diapositivas
Adjacency list por
Adjacency listAdjacency list
Adjacency listStefi Yu
4.5K vistas15 diapositivas
Graph Basic In Data structure por
Graph Basic In Data structureGraph Basic In Data structure
Graph Basic In Data structureIkhlas Rahman
2.1K vistas12 diapositivas
Graph in data structure por
Graph in data structureGraph in data structure
Graph in data structureAbrish06
43.2K vistas24 diapositivas
Graph Data Structure por
Graph Data StructureGraph Data Structure
Graph Data StructureAfaq Mansoor Khan
300 vistas32 diapositivas

Más contenido relacionado

La actualidad más candente

Graphs in Data Structure por
Graphs in Data StructureGraphs in Data Structure
Graphs in Data Structurelalithambiga kamaraj
748 vistas31 diapositivas
Graphs in data structures por
Graphs in data structuresGraphs in data structures
Graphs in data structuresSavit Chandra
1K vistas32 diapositivas
Graphs in data structure por
Graphs in data structureGraphs in data structure
Graphs in data structurehamza javed
1.8K vistas32 diapositivas
Graphs por
GraphsGraphs
GraphsZulkifal Yousaf
1.7K vistas30 diapositivas
Data structure computer graphs por
Data structure computer graphsData structure computer graphs
Data structure computer graphsKumar
4.4K vistas22 diapositivas
Graphs in Data Structure por
 Graphs in Data Structure Graphs in Data Structure
Graphs in Data Structurehafsa komal
2.2K vistas22 diapositivas

La actualidad más candente(20)

Graphs in data structure por hamza javed
Graphs in data structureGraphs in data structure
Graphs in data structure
hamza javed1.8K vistas
Data structure computer graphs por Kumar
Data structure computer graphsData structure computer graphs
Data structure computer graphs
Kumar 4.4K vistas
Graphs in Data Structure por hafsa komal
 Graphs in Data Structure Graphs in Data Structure
Graphs in Data Structure
hafsa komal2.2K vistas
Graphs in c language por SARITHA REDDY
Graphs in c languageGraphs in c language
Graphs in c language
SARITHA REDDY8.7K vistas
Data structure - Graph por Madhu Bala
Data structure - GraphData structure - Graph
Data structure - Graph
Madhu Bala6.4K vistas
Data Structure : Graph and Graph Traversing por Vikas Chandwani
Data Structure : Graph and Graph TraversingData Structure : Graph and Graph Traversing
Data Structure : Graph and Graph Traversing
Vikas Chandwani247 vistas
Skiena algorithm 2007 lecture10 graph data strctures por zukun
Skiena algorithm 2007 lecture10 graph data strcturesSkiena algorithm 2007 lecture10 graph data strctures
Skiena algorithm 2007 lecture10 graph data strctures
zukun2.2K vistas
Graphs In Data Structure por Anuj Modi
Graphs In Data StructureGraphs In Data Structure
Graphs In Data Structure
Anuj Modi45.6K vistas
2.1 graph basic por Krish_ver2
2.1 graph basic 2.1 graph basic
2.1 graph basic
Krish_ver21.2K vistas

Similar a Graph in data structure

Lecture 5b graphs and hashing por
Lecture 5b graphs and hashingLecture 5b graphs and hashing
Lecture 5b graphs and hashingVictor Palmar
1.2K vistas56 diapositivas
UNIT III.pptx por
UNIT III.pptxUNIT III.pptx
UNIT III.pptxSwarndeviKm
29 vistas74 diapositivas
logic.pptx por
logic.pptxlogic.pptx
logic.pptxKENNEDY GITHAIGA
4 vistas58 diapositivas
LEC 12-DSALGO-GRAPHS(final12).pdf por
LEC 12-DSALGO-GRAPHS(final12).pdfLEC 12-DSALGO-GRAPHS(final12).pdf
LEC 12-DSALGO-GRAPHS(final12).pdfMuhammadUmerIhtisham
77 vistas73 diapositivas
Graph 1 por
Graph 1Graph 1
Graph 1International Islamic University
272 vistas90 diapositivas

Similar a Graph in data structure(20)

Lecture 5b graphs and hashing por Victor Palmar
Lecture 5b graphs and hashingLecture 5b graphs and hashing
Lecture 5b graphs and hashing
Victor Palmar1.2K vistas
Class01_Computer_Contest_Level_3_Notes_Sep_07 - Copy.pdf por ChristianKapsales1
Class01_Computer_Contest_Level_3_Notes_Sep_07 - Copy.pdfClass01_Computer_Contest_Level_3_Notes_Sep_07 - Copy.pdf
Class01_Computer_Contest_Level_3_Notes_Sep_07 - Copy.pdf
358 33 powerpoint-slides_13-graphs_chapter-13 por sumitbardhan
358 33 powerpoint-slides_13-graphs_chapter-13358 33 powerpoint-slides_13-graphs_chapter-13
358 33 powerpoint-slides_13-graphs_chapter-13
sumitbardhan3.9K vistas
Graphical reprsentation dsa (DATA STRUCTURE ALGORITHM) por Abdul Naqashbandi
Graphical reprsentation dsa (DATA STRUCTURE ALGORITHM)Graphical reprsentation dsa (DATA STRUCTURE ALGORITHM)
Graphical reprsentation dsa (DATA STRUCTURE ALGORITHM)
Abdul Naqashbandi48 vistas

Más de Pooja Bhojwani

Internship msc cs por
Internship msc csInternship msc cs
Internship msc csPooja Bhojwani
112 vistas25 diapositivas
Networking (osi model) por
Networking (osi model)Networking (osi model)
Networking (osi model)Pooja Bhojwani
197 vistas30 diapositivas
Java(inheritance) por
Java(inheritance)Java(inheritance)
Java(inheritance)Pooja Bhojwani
181 vistas57 diapositivas
Online Complaint of Society por
Online Complaint of SocietyOnline Complaint of Society
Online Complaint of SocietyPooja Bhojwani
58 vistas14 diapositivas
gender bias por
gender biasgender bias
gender biasPooja Bhojwani
167 vistas33 diapositivas
Pooja bhojwani por
Pooja bhojwani Pooja bhojwani
Pooja bhojwani Pooja Bhojwani
37 vistas12 diapositivas

Último

ppt_dunarea.pptx por
ppt_dunarea.pptxppt_dunarea.pptx
ppt_dunarea.pptxvvvgeorgevvv
68 vistas5 diapositivas
Guidelines & Identification of Early Sepsis DR. NN CHAVAN 02122023.pptx por
Guidelines & Identification of Early Sepsis DR. NN CHAVAN 02122023.pptxGuidelines & Identification of Early Sepsis DR. NN CHAVAN 02122023.pptx
Guidelines & Identification of Early Sepsis DR. NN CHAVAN 02122023.pptxNiranjan Chavan
43 vistas48 diapositivas
ICS3211_lecture 09_2023.pdf por
ICS3211_lecture 09_2023.pdfICS3211_lecture 09_2023.pdf
ICS3211_lecture 09_2023.pdfVanessa Camilleri
150 vistas10 diapositivas
Pharmaceutical Analysis PPT (BP 102T) por
Pharmaceutical Analysis PPT (BP 102T) Pharmaceutical Analysis PPT (BP 102T)
Pharmaceutical Analysis PPT (BP 102T) yakshpharmacy009
118 vistas29 diapositivas
Berry country.pdf por
Berry country.pdfBerry country.pdf
Berry country.pdfMariaKenney3
82 vistas12 diapositivas
Payment Integration using Braintree Connector | MuleSoft Mysore Meetup #37 por
Payment Integration using Braintree Connector | MuleSoft Mysore Meetup #37Payment Integration using Braintree Connector | MuleSoft Mysore Meetup #37
Payment Integration using Braintree Connector | MuleSoft Mysore Meetup #37MysoreMuleSoftMeetup
55 vistas17 diapositivas

Último(20)

Guidelines & Identification of Early Sepsis DR. NN CHAVAN 02122023.pptx por Niranjan Chavan
Guidelines & Identification of Early Sepsis DR. NN CHAVAN 02122023.pptxGuidelines & Identification of Early Sepsis DR. NN CHAVAN 02122023.pptx
Guidelines & Identification of Early Sepsis DR. NN CHAVAN 02122023.pptx
Niranjan Chavan43 vistas
Pharmaceutical Analysis PPT (BP 102T) por yakshpharmacy009
Pharmaceutical Analysis PPT (BP 102T) Pharmaceutical Analysis PPT (BP 102T)
Pharmaceutical Analysis PPT (BP 102T)
yakshpharmacy009118 vistas
Payment Integration using Braintree Connector | MuleSoft Mysore Meetup #37 por MysoreMuleSoftMeetup
Payment Integration using Braintree Connector | MuleSoft Mysore Meetup #37Payment Integration using Braintree Connector | MuleSoft Mysore Meetup #37
Payment Integration using Braintree Connector | MuleSoft Mysore Meetup #37
GSoC 2024 .pdf por ShabNaz2
GSoC 2024 .pdfGSoC 2024 .pdf
GSoC 2024 .pdf
ShabNaz245 vistas
Presentation_NC_Future now 2006.pdf por Lora
Presentation_NC_Future now 2006.pdfPresentation_NC_Future now 2006.pdf
Presentation_NC_Future now 2006.pdf
Lora 38 vistas
The Future of Micro-credentials: Is Small Really Beautiful? por Mark Brown
The Future of Micro-credentials:  Is Small Really Beautiful?The Future of Micro-credentials:  Is Small Really Beautiful?
The Future of Micro-credentials: Is Small Really Beautiful?
Mark Brown121 vistas
UNIT NO 13 ORGANISMS AND POPULATION.pptx por Madhuri Bhande
UNIT NO 13 ORGANISMS AND POPULATION.pptxUNIT NO 13 ORGANISMS AND POPULATION.pptx
UNIT NO 13 ORGANISMS AND POPULATION.pptx
Madhuri Bhande48 vistas
BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (FRIE... por Nguyen Thanh Tu Collection
BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (FRIE...BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (FRIE...
BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (FRIE...
What is Digital Transformation? por Mark Brown
What is Digital Transformation?What is Digital Transformation?
What is Digital Transformation?
Mark Brown46 vistas
INT-244 Topic 6b Confucianism por S Meyer
INT-244 Topic 6b ConfucianismINT-244 Topic 6b Confucianism
INT-244 Topic 6b Confucianism
S Meyer51 vistas
JRN 362 - Lecture Twenty-Three (Epilogue) por Rich Hanley
JRN 362 - Lecture Twenty-Three (Epilogue)JRN 362 - Lecture Twenty-Three (Epilogue)
JRN 362 - Lecture Twenty-Three (Epilogue)
Rich Hanley44 vistas

Graph in data structure

  • 5. A graph G = (V,E) is composed of: • V: set of vertices(or points or nodes) • E: set of edges connecting the vertices in v or (A set E of edges such that each edge e in E is identified with a unique (unordered) pair [u, v] of nodes in V, denotes by e = [u, v]) * example= V= {a,b,c,d,e} E= {(a,b),(a,c),(a,d), (b,e),(c,d),(c,e), (d,e)}
  • 6. Graphs cont..  If the element are in ordered pairs it is call directed graph.  If the pairs represent the same edge it is known as undirected graph.  Subgraph happened when every vertex include in other vertex and every edge is include in other edge. 1 2 3 4 5 1 2 3 4 5
  • 7. Some of the applications of graphs are : • Mazes (using stacks) • Networks (computer, cities ....) • Maps (any geographic databases ) • Graphics : Geometrical Objects • Neighborhood graphs • Voronoi diagrams
  • 8. Many geometrical objects such as cubes, polyhedral, and wire frame car models, may be thought of as graphs. These graphs are more than just nodes and edges. Their geometrical structure must also be taken into account. For example consider the cube: • This structure contains three kinds of objects • vertices • edges • faces Edges are crucial since, in a three dimensional object, an edge will always belong to only two faces and two nodes. For this reason it makes sense to number the edges. Faces become linked list of edges, and each edge lives in only two faces
  • 10.  Adjacency Matrix Representation  An adjacency matrix is one of the two common ways to represent a graph.  The adjacency matrix shows which nodes are adjacent to one another. Two nodes are adjacent if there is an edge connecting them.  In the case of a directed graph, if node j is adjacent to node i, there is an edge from i to j. In other words, if j is adjacent to i, you can get from i to j by traversing one edge.  For a given graph with n nodes, the adjacency matrix will have dimensions of n x n.  For an unweighted graph, the adjacency matrix will be populated with boolean values.
  • 11. • For any given node i, you can determine its adjacent nodes by looking at row of the adjacency matrix. A value of true at indicates that there is an edge from node i to node j, and false indicating no edge. In an undirected graph, the values of and will be equal. • In a weighted graph, the boolean values will be replaced by the weight of the edge connecting the two nodes, with a special value that indicates the absence of an edge. • The memory use of an adjacency matrix is O(n2).
  • 12. Adjacency Matrix A B C D E A 0 0 0 1 0 B 0 0 0 1 0 C 0 1 0 0 0 D 0 0 0 0 1 E 0 0 1 0 0
  • 13. Adjacency List Representation • One way to have the graph maintain a list of lists, in which the first list is a list of indices corresponding to each node in the graph. • Each of these refer to another list that stores a the index of each adjacent node to this one. It also useful to associate the weight of each link with the adjacent node in this list. Example: An undirected graph contains four nodes 1, 2, 3 and 4. 1 is linked to 2 and 3. 2 is linked to 3. 3 is linked to 4. 1 - [2, 3] 2 - [1, 3] 3 - [1, 2, 4] 4 - [3]
  • 14. Adjacency List Representation • It useful to store the list of all the nodes in the graph in a hash table. The keys then would correspond to the indices of each node and the value would be a reference to the list of adjacent node indecies. • Another implementation might require that each node keep a list of its adjacent nodes.
  • 16. Graph Traversal •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)
  • 17. • Start at vertex v, visit its neighbor w, then w's neighbor y and keep going until reach 'a dead end' then iterate back and visit nodes reachable from second last visited vertex and keep applying the same principle. • General algorithm: for each vertex v in the graph if v is not visited start the depth first traversal at v 17
  • 18. 0 1 2 4 5 6 3 7 8 9 10 Directed graph G3
  • 19. The depth first ordering of the vertices graph G3: 0 1 2 3 4 5 6 8 10 7 9 The general algorithm to do a depth first traversal at a given node v is: 1. Mark node v as visited 2. Visit the node 3. For each vertex u adjacent to v if u is not visited start the depth first traversal at u
  • 20. Breadth-First Search (Queue) Breadth first search visits the nodes neighbours and then the univisited neighbours of the neighbours etc. If it starts on vertex a it will go to all vertices that have an edge from a. If some points are not reachable it will have to start another BFS from a new vertex. Base on the same example in Depth-First Traversal : 0 1 2 3 4 5 6 8 10 7 9 Start the traversal at vertex 0. After visiting vertex 0 next visit the vertices that are directly connected to it, that are 1 and 5. Next visit the vertices that are directed connected to 1 and are not visit, that is 2 and 3. Then visit vertices that directly connected to 5 and are not visited, that is 6. Then continue to next path.
  • 21. Breadth-First Search The general algorithm: a. for each vertex v in the graph if v is not visited add v to the queue // start the breadth first search at v b. Mark v as visited c. While the queue is not empty c.1. Remove vertex u from the queue c.2. Retrieve the vertices adjacent to u c.3. for each vertex w that is adjacent to u if w is not visited c.3.1. Add w to the queue c.3.2. Mark w as visited
  • 22. • We may also want to associate some cost or weight to the traversal of an edge. When we add this information, the graph is called weighted. An example of a weighted graph would be the distance between the capitals of a set of countries. • Directed and undirected graphs may both be weighted. The operations on a weighted graph are the same with addition of a weight parameter during edge creation: • Weighted Graph Operations (an extension of undirected/directed graph operations) • make-edge(vertex u, vertex v, weight w): edge • Create an edge between u and v with weight w. In a directed graph, the edge will flow from u to v. • Weight graph also being used to find the shortest path.
  • 24. Shortest Path Shortest path A-B is 2 Shortest path from D to A is (DA) = 1 Shortest path from C to A is (CBA) = 3 Shortest path from E to A is (EDA) = 4 Shortest path from F to A is (FCBA) = 5
  • 25. Minimum Spanning Tree 25 A spanning tree of a graph is just a subgraph that contains all the vertices and is a tree. A graph may have many spanning trees; for instance the complete graph on four vertices fom the figure of graph k4 ..it has sixteen spanning trees:
  • 26. 26