Data structure computer graphs

Kumar
Kumar Administrator
Graphs
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
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)
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)
Directed vs. undirected graphs
(cont.)
E(Graph2) = {(1,3) (3,1) (5,9) (9,11)
(5,7)
Warning: if the graph is
directed, the order of the
vertices in each edge is
important !!
• Trees are special cases of graphs!!
Trees vs graphs
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
5 is adjacent to 7
7 is adjacent from 5
• What is the number of edges in a complete
directed graph with N vertices?
N * (N-1)
Graph terminology (cont.)
• What is the number of edges in a complete
undirected graph with N vertices?
N * (N-1) / 2
Graph terminology (cont.)
Graph terminology (cont.)
• Multi-graph
• Cycle
• Loop
• Weighted graph: a graph in which each edge
carries a value
Graph terminology (cont.)
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
Array-based implementation-
Adjacency Matrix
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)
Linked-list implementation
Adjacency Matrix for Un-
Weighted Graphs
1. Adjacency Matrix
2. Matrix for edges
3. Matrix for 2 length paths
4. Matrix for 3 length paths
5. Matrix for n length paths
6. Path Matrix
7. Completely Connected Graph
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)
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
DFS
• Algorithm:
Starting Node is A
1. Initialize all nodes to ready state(Status=1)
2. Push starting nose A onto Stack and change its status to
waiting state (Status=2).
3.Repeat steps 4 & 5 until STACK is empty.
4. Pop Top node N. Process N and change its Status to the processed
state (Status=3).
5.Push onto Stack all the neighbors of N that are still in ready state
and change their state as waiting state(State=2).
6. Exit.
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)
BFS
• Algorithm:
Starting Node is A
1. Initialize all nodes to ready state(Status=1)
2. Push starting nose A onto Queue and change its status to
waiting state (Status=2).
3.Repeat steps 4 & 5 until Queue is empty.
4. Remove the front node N. Process N and change its Status to the
processed state (Status=3).
5.Add to the rear of Queue all the neighbors of N that are still in
ready state and change their state to waiting (State=2).
6. Exit.
• Examples of Both DFS & BFS.
1 de 22

Recomendados

Graph representationGraph representation
Graph representationTech_MX
37.2K vistas34 diapositivas
Graph in data structureGraph in data structure
Graph in data structureAbrish06
42.7K vistas24 diapositivas
Graphs In Data StructureGraphs In Data Structure
Graphs In Data StructureAnuj Modi
45.6K vistas32 diapositivas
Graphs in data structureGraphs in data structure
Graphs in data structurehamza javed
1.8K vistas32 diapositivas
 Graphs in Data Structure Graphs in Data Structure
Graphs in Data Structurehafsa komal
2.2K vistas22 diapositivas

Más contenido relacionado

La actualidad más candente

Graph Basic In Data structureGraph Basic In Data structure
Graph Basic In Data structureIkhlas Rahman
2.1K vistas12 diapositivas
Graph  theoryGraph  theory
Graph theoryManash Kumar Mondal
4K vistas42 diapositivas
Hamiltonian pathHamiltonian path
Hamiltonian pathArindam Ghosh
8.7K vistas8 diapositivas
introduction to graph theoryintroduction to graph theory
introduction to graph theoryChuckie Balbuena
21.6K vistas49 diapositivas
graph theory graph theory
graph theory ganith2k13
14.5K vistas93 diapositivas

La actualidad más candente(20)

Graph Basic In Data structureGraph Basic In Data structure
Graph Basic In Data structure
Ikhlas Rahman2.1K vistas
Graph  theoryGraph  theory
Graph theory
Manash Kumar Mondal4K vistas
Hamiltonian pathHamiltonian path
Hamiltonian path
Arindam Ghosh8.7K vistas
Introduction to Graph TheoryIntroduction to Graph Theory
Introduction to Graph Theory
Premsankar Chakkingal35K vistas
introduction to graph theoryintroduction to graph theory
introduction to graph theory
Chuckie Balbuena21.6K vistas
graph theory graph theory
graph theory
ganith2k1314.5K vistas
Graph theoryGraph theory
Graph theory
Thirunavukarasu Mani9.9K vistas
Spanning treesSpanning trees
Spanning trees
Shareb Ismaeel9.3K vistas
GraphsGraphs
Graphs
Mohd Arif5.6K vistas
Kruskal’s algorithmKruskal’s algorithm
Kruskal’s algorithm
Abdul Moiz Lakhani1.9K vistas
Graph TheoryGraph Theory
Graph Theory
Ehsan Hamzei4.1K vistas
Graph Traversal AlgorithmGraph Traversal Algorithm
Graph Traversal Algorithm
jyothimonc7.3K vistas
DijkstraDijkstra
Dijkstra
jagdeeparora866.6K vistas
Graph traversals in Data StructuresGraph traversals in Data Structures
Graph traversals in Data Structures
Anandhasilambarasan D4.3K vistas
Lecture8 data structure(graph)Lecture8 data structure(graph)
Lecture8 data structure(graph)
Taibah University, College of Computer Science & Engineering20.2K vistas
Hamilton path and euler pathHamilton path and euler path
Hamilton path and euler path
Shakib Sarar Arnab3.6K vistas
Data structure - GraphData structure - Graph
Data structure - Graph
Madhu Bala6.2K vistas
Algorithms Lecture 7: Graph AlgorithmsAlgorithms Lecture 7: Graph Algorithms
Algorithms Lecture 7: Graph Algorithms
Benha University3.6K vistas
Slides Chapter10.1 10.2Slides Chapter10.1 10.2
Slides Chapter10.1 10.2
showslidedump3.1K vistas
Graph Theory: TreesGraph Theory: Trees
Graph Theory: Trees
Ashikur Rahman1.4K vistas

Destacado

Graph data structureGraph data structure
Graph data structureTech_MX
14.1K vistas28 diapositivas
Trees data structureTrees data structure
Trees data structureSumit Gupta
19.4K vistas18 diapositivas
Graph theory 1Graph theory 1
Graph theory 1Tech_MX
7.9K vistas43 diapositivas
DATA STRUCTURESDATA STRUCTURES
DATA STRUCTURESbca2010
96.8K vistas86 diapositivas
17. Trees and Graphs17. Trees and Graphs
17. Trees and GraphsIntro C# Book
259.1K vistas86 diapositivas

Destacado(19)

Graph data structureGraph data structure
Graph data structure
Tech_MX14.1K vistas
Matrix Representation Of GraphMatrix Representation Of Graph
Matrix Representation Of Graph
Abhishek Pachisia26K vistas
Trees data structureTrees data structure
Trees data structure
Sumit Gupta19.4K vistas
Graph theory 1Graph theory 1
Graph theory 1
Tech_MX7.9K vistas
DATA STRUCTURESDATA STRUCTURES
DATA STRUCTURES
bca201096.8K vistas
17. Trees and Graphs17. Trees and Graphs
17. Trees and Graphs
Intro C# Book259.1K vistas
Data Structures, GraphsData Structures, Graphs
Data Structures, Graphs
Jibrael Jos482 vistas
GraphssGraphss
Graphss
fika sweety4.1K vistas
Data structure introductionData structure introduction
Data structure introduction
ramyasanthosh575 vistas
Graphs in data structuresGraphs in data structures
Graphs in data structures
Savit Chandra1K vistas
GraphsGraphs
Graphs
Saurabh Mishra1.5K vistas
Test casesTest cases
Test cases
Nataly Chill2.7K vistas
Path cycle part1Path cycle part1
Path cycle part1
guestb639412K vistas
Test Case Management with MTM 2013Test Case Management with MTM 2013
Test Case Management with MTM 2013
Raluca Suditu6.1K vistas
Graph Processing with Apache TinkerPopGraph Processing with Apache TinkerPop
Graph Processing with Apache TinkerPop
Jason Plurad1.4K vistas
Software Verification, Validation and TestingSoftware Verification, Validation and Testing
Software Verification, Validation and Testing
Dr Sukhpal Singh Gill 1.5K vistas
BlackboxBlackbox
Blackbox
GuruKrishnaTeja4.5K vistas

Similar a Data structure computer graphs

Graph Data StructureGraph Data Structure
Graph Data StructureAfaq Mansoor Khan
299 vistas32 diapositivas
Data structureData structure
Data structurejagjot singh chopra
265 vistas17 diapositivas
Graph in data structureGraph in data structure
Graph in data structurePooja Bhojwani
196 vistas28 diapositivas
ppt 1.pptxppt 1.pptx
ppt 1.pptxShasidharaniD
5 vistas82 diapositivas
Graphs (1)Graphs (1)
Graphs (1)Meet Patel
150 vistas13 diapositivas

Similar a Data structure computer graphs(20)

LEC 12-DSALGO-GRAPHS(final12).pdfLEC 12-DSALGO-GRAPHS(final12).pdf
LEC 12-DSALGO-GRAPHS(final12).pdf
MuhammadUmerIhtisham77 vistas
Graph Data StructureGraph Data Structure
Graph Data Structure
Afaq Mansoor Khan299 vistas
Data structureData structure
Data structure
jagjot singh chopra265 vistas
Graph in data structureGraph in data structure
Graph in data structure
Pooja Bhojwani196 vistas
ppt 1.pptxppt 1.pptx
ppt 1.pptx
ShasidharaniD5 vistas
Graphs (1)Graphs (1)
Graphs (1)
Meet Patel150 vistas
Unit   ix    graph Unit   ix    graph
Unit ix graph
Tribhuvan University986 vistas
Unit   9 graphUnit   9 graph
Unit 9 graph
Dabbal Singh Mahara222 vistas
GraphsGraphs
Graphs
LavanyaJ2824 vistas
Graphs.pptGraphs.ppt
Graphs.ppt
lakshmi2635571 vistas
6. Graphs6. Graphs
6. Graphs
Mandeep Singh100 vistas
Unit V - ppt.pptxUnit V - ppt.pptx
Unit V - ppt.pptx
Kongunadu College of Engineering and Technology9 vistas
GraphsGraphs
Graphs
KomalPaliwal366 vistas
Graphs and eularian circuit & path with c++ programGraphs and eularian circuit & path with c++ program
Graphs and eularian circuit & path with c++ program
Muhammad Danish Badar2.9K vistas
UNIT III.pptxUNIT III.pptx
UNIT III.pptx
SwarndeviKm29 vistas
Graph TheoryGraph Theory
Graph Theory
Rashmi Bhat298 vistas
U1 L5 DAA.pdfU1 L5 DAA.pdf
U1 L5 DAA.pdf
LakshyaBaliyan22 vistas
Graph 1Graph 1
Graph 1
International Islamic University272 vistas
10.graph10.graph
10.graph
jashobhan pradhan918 vistas

Más de Kumar

Graphics devicesGraphics devices
Graphics devicesKumar
6.6K vistas67 diapositivas
Fill area algorithmsFill area algorithms
Fill area algorithmsKumar
22.1K vistas45 diapositivas
region-fillingregion-filling
region-fillingKumar
9.2K vistas81 diapositivas
Bresenham derivationBresenham derivation
Bresenham derivationKumar
1K vistas3 diapositivas
Introductionto xsltIntroductionto xslt
Introductionto xsltKumar
1.3K vistas63 diapositivas

Más de Kumar (20)

Graphics devicesGraphics devices
Graphics devices
Kumar 6.6K vistas
Fill area algorithmsFill area algorithms
Fill area algorithms
Kumar 22.1K vistas
region-fillingregion-filling
region-filling
Kumar 9.2K vistas
Bresenham derivationBresenham derivation
Bresenham derivation
Kumar 1K vistas
Introductionto xsltIntroductionto xslt
Introductionto xslt
Kumar 1.3K vistas
Extracting data from xmlExtracting data from xml
Extracting data from xml
Kumar 765 vistas
Xml basicsXml basics
Xml basics
Kumar 958 vistas
XML SchemaXML Schema
XML Schema
Kumar 4.3K vistas
Publishing xmlPublishing xml
Publishing xml
Kumar 468 vistas
DTDDTD
DTD
Kumar 1.1K vistas
Applying xmlApplying xml
Applying xml
Kumar 492 vistas
Introduction to XMLIntroduction to XML
Introduction to XML
Kumar 918 vistas
JNDI, JMS, JPA, XMLJNDI, JMS, JPA, XML
JNDI, JMS, JPA, XML
Kumar 738 vistas
EJB FundmentalsEJB Fundmentals
EJB Fundmentals
Kumar 336 vistas
JSP and struts programmingJSP and struts programming
JSP and struts programming
Kumar 1.2K vistas
Introduction to J2EEIntroduction to J2EE
Introduction to J2EE
Kumar 420 vistas

Data structure computer 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
  • 3. Formal definition of graphs • A graph G is defined as follows: G=(V,E) V(G): a finite, nonempty set of vertices E(G): a set of edges (pairs of vertices)
  • 4. Directed vs. undirected graphs • When the edges in a graph have no direction, the graph is called undirected
  • 5. • When the edges in a graph have a direction, the graph is called directed (or digraph) Directed vs. undirected graphs (cont.) E(Graph2) = {(1,3) (3,1) (5,9) (9,11) (5,7) Warning: if the graph is directed, the order of the vertices in each edge is important !!
  • 6. • Trees are special cases of graphs!! Trees vs graphs
  • 7. 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 5 is adjacent to 7 7 is adjacent from 5
  • 8. • What is the number of edges in a complete directed graph with N vertices? N * (N-1) Graph terminology (cont.)
  • 9. • What is the number of edges in a complete undirected graph with N vertices? N * (N-1) / 2 Graph terminology (cont.)
  • 10. Graph terminology (cont.) • Multi-graph • Cycle • Loop
  • 11. • Weighted graph: a graph in which each edge carries a value Graph terminology (cont.)
  • 12. 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
  • 14. 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)
  • 16. Adjacency Matrix for Un- Weighted Graphs 1. Adjacency Matrix 2. Matrix for edges 3. Matrix for 2 length paths 4. Matrix for 3 length paths 5. Matrix for n length paths 6. Path Matrix 7. Completely Connected Graph
  • 17. 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)
  • 18. 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
  • 19. DFS • Algorithm: Starting Node is A 1. Initialize all nodes to ready state(Status=1) 2. Push starting nose A onto Stack and change its status to waiting state (Status=2). 3.Repeat steps 4 & 5 until STACK is empty. 4. Pop Top node N. Process N and change its Status to the processed state (Status=3). 5.Push onto Stack all the neighbors of N that are still in ready state and change their state as waiting state(State=2). 6. Exit.
  • 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. BFS • Algorithm: Starting Node is A 1. Initialize all nodes to ready state(Status=1) 2. Push starting nose A onto Queue and change its status to waiting state (Status=2). 3.Repeat steps 4 & 5 until Queue is empty. 4. Remove the front node N. Process N and change its Status to the processed state (Status=3). 5.Add to the rear of Queue all the neighbors of N that are still in ready state and change their state to waiting (State=2). 6. Exit.
  • 22. • Examples of Both DFS & BFS.