SlideShare a Scribd company logo
1 of 25
Graphs in ‘C’ Language



              By
         Dr. C. Saritha
    Lecturer in Electronics
SSBN Degree College, Anantapur
Data Structure
• Data structure is a particular way of
  sorting and organizing data in a
  computer. So that it can be used
  efficiently.
• Different kinds of data structures are
  suited to different kinds of applications.
Types of Data Structure
• Linear Data Structure: A data structure
  is said to be linear data structure if its
  elements form a sequence.
  Ex: Array, Stack , Queue and linked list.
• Non-Linear Data Structure: Elements in
  a non-linear data structure do not form
  a sequence.
  Ex: Tree and Graph.
Types of data structure

                                     Data Structures




                    Linear                                     Non-Linear




Arrays   Linked lists        Stack       Queue         Trees            Graphs
Trees
• A tree is hierarchical collection of
  nodes. One of the nodes, known as the
  root, is at the top of the hierarchical.
• Each node can have utmost one link
  coming into it.
• The node where the link originates is
  called the parent node. The root node
  has no parent.
Continue…
• The links leaving a node (any number
  of links are allowed) point to child
  nodes.
                  A

       B            C          D

   E       F G H          I     J
Graphs
• A graph is a set of nodes (also called
  vertices) and a set of arcs (also called
  edges). A sample graph is as fallows:

           A            B


                    C
                              D
               E
Edges & Vertices

• Each node in a graph is known as a
  vertex of the graph.
• The Each edge of a graph contains two
  vertices.
Types of Graphs
• The Graphs can be classified into two
  types. They are:
Undirected graphs
Directed graph
Continue…
Undirected graphs: In an undirected
 graph the order of pair is unimportant.
 Hence the pairs (v1,v2)and (v2,v1)
 represent same edge.
Directed graph: In this the order of the
 vertices representing an edge is
 important. This pair is represented as
 <v1,v2> where v1 is the tail and v2 is
 head of v2. Thus <v1,v2> and <v2,v1>
 represents different edges.
Undirected graph

           1            2

                                   5


           3            4

• Set of vertices={1,2,3,4,5}
• Set of edges= {(1,2),(1,3),(1,4),(2,3),
                   (2,4),(2,5),(3,4),(4,5)}
Directed Graphs
                 1


                 2


                 3


• Set of vertices={1,2,3}
• Set of edges= {<1,2>, <2,1>, <2,3>,
                          <3,2>}
Adjacent Vectors & Incident Edges
• In an undirected graph if (v1,v2) is an
  edge in the set of edges, then the
  vertices v1 and v2 are said to be
  adjacent and the edge (v1,v2) is
  incident on vertices v1 and v2.
• If <v1,v2> is a directed edge, then
  vertex v1 is said to be adjacent to v2
  while v2 is adjacent from v1.
• The edge <v1,v2> is incident to v1 and
  v2.
Continue…
• The vertex 2 in the
  shown      undirected
  graph is adjacent to    1      2
  vertices 1,3,4 and
  5.The edges incident                    5
  on vertex 3 are
  (1,3),(2,3)       and   3      4
  (3,4).
Continue…
• In     the   shown
  directed graph the     1
  edges incident to
  vertex     2    are    2
  <1,2>,<2,1>,<2,3>
  and <3,2>.
                         3
Terminology of Graphs
• Weighted graph: A
  graph is said to be
  weighted graph if
  it’s edges have been           2
                                     2
  assigned some non-         1
  negative value as
                         5
  weight. A weighted                     6
  graph is known as
  network.                   3       4
                                 3
Continue…
• Degree:     In     an
  undirected     graph,
  the number of edges     1        2
  connected to a node
  is called the degree
  of that node. Where
  is in digraph, there             4
                          3
  are two degrees for
  every node they are
  indegree          and
  outdegree.
Continue…
• Indegree:       The
  indegree of node is
  the number of edges    1         2
  coming    to    that
  node.
• Out degree: The out
  degree of node is      3         4
  the number of edges
  going outside that
  node.
Continue…
• Isolated node: If any node has no
  edges connected with any other node
  then its degree will be zero (0) and it
  will be called as isolated node.
           A           B


                   C
                              D
               E
Continue…
• Source: A node,
  which       has     no
  incoming edges, but      1        2
  has outgoing edges,
  is called a source.

                           3        4
Continue…
• Connected      graph:
  An undirected graph                 B
                          A
  is   said    to    be
  connected if there is
  a path from any                 C
  node of graph to                            D
  any other node.             E
Continue…
• Complete graph: An
  undirected     graph               1
  will contain n(n-1)/2    1
  edges is called a
  complete       graph     2         2
  where as in the case
  of     digraph    will
  contain        n(n-1)    3
                                     3
  edges, where n is
  the total number of
  nodes in the graph.
Continue…
• Loop: An edge will
  be called loop or self
  edge if it starts and    1          2
  ends on the same
  node.

                           3          4
Breadth first search
• The general idea
  behind a breadth
  first       traversal    A     B    C
  beginning     at    a
  starting node A is as    D     E
  following. We first
  examine the starting
  node A, and then its     F      G
  neighbors after that
  the neighbors of its
  neighbors.            • A BDE CFG
Depth first search
• In Depth first search
  technique also we take      T      H   A
  one node as starting
  node. Then go to the           K   Y   N
  path     which     from
  starting node and visit
  all the nodes which are     O
  in the path. When we
  reach at the last node         U
  then     we    traverse
  another path starting     THANK YOU
  from that node

More Related Content

What's hot (20)

Binary Tree Traversal
Binary Tree TraversalBinary Tree Traversal
Binary Tree Traversal
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquer
 
Lecture8 data structure(graph)
Lecture8 data structure(graph)Lecture8 data structure(graph)
Lecture8 data structure(graph)
 
Introduction to Graph Theory
Introduction to Graph TheoryIntroduction to Graph Theory
Introduction to Graph Theory
 
Introduction to data structure ppt
Introduction to data structure pptIntroduction to data structure ppt
Introduction to data structure ppt
 
Graphs
GraphsGraphs
Graphs
 
sorting and its types
sorting and its typessorting and its types
sorting and its types
 
DESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMSDESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMS
 
1.1 binary tree
1.1 binary tree1.1 binary tree
1.1 binary tree
 
Graph theory presentation
Graph theory presentationGraph theory presentation
Graph theory presentation
 
Depth first search [dfs]
Depth first search [dfs]Depth first search [dfs]
Depth first search [dfs]
 
Breadth first search and depth first search
Breadth first search and  depth first searchBreadth first search and  depth first search
Breadth first search and depth first search
 
Floyd Warshall Algorithm
Floyd Warshall AlgorithmFloyd Warshall Algorithm
Floyd Warshall Algorithm
 
DFS & BFS Graph
DFS & BFS GraphDFS & BFS Graph
DFS & BFS Graph
 
Tree traversal techniques
Tree traversal techniquesTree traversal techniques
Tree traversal techniques
 
Graphs
GraphsGraphs
Graphs
 
DFS and BFS
DFS and BFSDFS and BFS
DFS and BFS
 
Topological Sorting
Topological SortingTopological Sorting
Topological Sorting
 
Quick sort-Data Structure
Quick sort-Data StructureQuick sort-Data Structure
Quick sort-Data Structure
 
15 puzzle problem using branch and bound
15 puzzle problem using branch and bound15 puzzle problem using branch and bound
15 puzzle problem using branch and bound
 

Viewers also liked (13)

C programming for Graphs
C programming for GraphsC programming for Graphs
C programming for Graphs
 
Dijkstra c
Dijkstra cDijkstra c
Dijkstra c
 
digital logic_families
digital logic_familiesdigital logic_families
digital logic_families
 
logic family
logic familylogic family
logic family
 
Logic families
Logic  familiesLogic  families
Logic families
 
logic families
logic familieslogic families
logic families
 
Logic families
Logic familiesLogic families
Logic families
 
Laser and its medical applications
Laser and its medical applicationsLaser and its medical applications
Laser and its medical applications
 
Logic gates
Logic gatesLogic gates
Logic gates
 
17. Trees and Graphs
17. Trees and Graphs17. Trees and Graphs
17. Trees and Graphs
 
The logic gate circuit
The logic gate circuitThe logic gate circuit
The logic gate circuit
 
How to Make Awesome SlideShares: Tips & Tricks
How to Make Awesome SlideShares: Tips & TricksHow to Make Awesome SlideShares: Tips & Tricks
How to Make Awesome SlideShares: Tips & Tricks
 
Getting Started With SlideShare
Getting Started With SlideShareGetting Started With SlideShare
Getting Started With SlideShare
 

Similar to Graphs in c language (20)

Graph data structure
Graph data structureGraph data structure
Graph data structure
 
Depth first traversal(data structure algorithms)
Depth first traversal(data structure algorithms)Depth first traversal(data structure algorithms)
Depth first traversal(data structure algorithms)
 
10.graph
10.graph10.graph
10.graph
 
ppt 1.pptx
ppt 1.pptxppt 1.pptx
ppt 1.pptx
 
Graph Theory
Graph TheoryGraph Theory
Graph Theory
 
Graph in data structure
Graph in data structureGraph in data structure
Graph in data structure
 
Graph theory
Graph theoryGraph theory
Graph theory
 
Graphs In Data Structure
Graphs In Data StructureGraphs In Data Structure
Graphs In Data Structure
 
Data structure computer graphs
Data structure computer graphsData structure computer graphs
Data structure computer graphs
 
Lecture8 data structure(graph)
Lecture8 data structure(graph)Lecture8 data structure(graph)
Lecture8 data structure(graph)
 
Unit-6 Graph.ppsx ppt
Unit-6 Graph.ppsx                                       pptUnit-6 Graph.ppsx                                       ppt
Unit-6 Graph.ppsx ppt
 
Unit 2: All
Unit 2: AllUnit 2: All
Unit 2: All
 
Graphs in data structures
Graphs in data structuresGraphs in data structures
Graphs in data structures
 
DSA-Unit-2.pptx
DSA-Unit-2.pptxDSA-Unit-2.pptx
DSA-Unit-2.pptx
 
Unit 9 graph
Unit   9 graphUnit   9 graph
Unit 9 graph
 
Unit ix graph
Unit   ix    graph Unit   ix    graph
Unit ix graph
 
Daa chpater 12
Daa chpater 12Daa chpater 12
Daa chpater 12
 
Graph theory
Graph theoryGraph theory
Graph theory
 
Unit VI - Graphs.ppt
Unit VI - Graphs.pptUnit VI - Graphs.ppt
Unit VI - Graphs.ppt
 
Tree
TreeTree
Tree
 

More from SARITHA REDDY

Unit iv microcontrollers final
Unit iv microcontrollers finalUnit iv microcontrollers final
Unit iv microcontrollers finalSARITHA REDDY
 
Introduction to microprocessors notes
Introduction to microprocessors notesIntroduction to microprocessors notes
Introduction to microprocessors notesSARITHA REDDY
 
8051 data types and directives
8051 data types and directives8051 data types and directives
8051 data types and directivesSARITHA REDDY
 
Unit ii microcontrollers final
Unit ii microcontrollers finalUnit ii microcontrollers final
Unit ii microcontrollers finalSARITHA REDDY
 
Introduction to microprocessor notes
Introduction to microprocessor notesIntroduction to microprocessor notes
Introduction to microprocessor notesSARITHA REDDY
 
8051 data type and directives
8051 data type and directives8051 data type and directives
8051 data type and directivesSARITHA REDDY
 
Decimation in time and frequency
Decimation in time and frequencyDecimation in time and frequency
Decimation in time and frequencySARITHA REDDY
 
Addressing modes of 8051
Addressing modes of 8051Addressing modes of 8051
Addressing modes of 8051SARITHA REDDY
 
I o ports and timers of 8051
I o ports and timers of 8051I o ports and timers of 8051
I o ports and timers of 8051SARITHA REDDY
 
Mos and cmos technology
Mos and cmos technologyMos and cmos technology
Mos and cmos technologySARITHA REDDY
 
Clampers and clippers
Clampers and clippersClampers and clippers
Clampers and clippersSARITHA REDDY
 
Electro Magnetic Wave Propagation
Electro Magnetic Wave PropagationElectro Magnetic Wave Propagation
Electro Magnetic Wave PropagationSARITHA REDDY
 
Satellite communications
Satellite communicationsSatellite communications
Satellite communicationsSARITHA REDDY
 
Electronics in daily life
Electronics in daily lifeElectronics in daily life
Electronics in daily lifeSARITHA REDDY
 
BSc I year practicals
BSc I year practicalsBSc I year practicals
BSc I year practicalsSARITHA REDDY
 
Combinational circuits
Combinational circuitsCombinational circuits
Combinational circuitsSARITHA REDDY
 
Applications of op amps
Applications of op ampsApplications of op amps
Applications of op ampsSARITHA REDDY
 

More from SARITHA REDDY (20)

Unit iv microcontrollers final
Unit iv microcontrollers finalUnit iv microcontrollers final
Unit iv microcontrollers final
 
Introduction to microprocessors notes
Introduction to microprocessors notesIntroduction to microprocessors notes
Introduction to microprocessors notes
 
8051 data types and directives
8051 data types and directives8051 data types and directives
8051 data types and directives
 
Unit ii microcontrollers final
Unit ii microcontrollers finalUnit ii microcontrollers final
Unit ii microcontrollers final
 
Introduction to microprocessor notes
Introduction to microprocessor notesIntroduction to microprocessor notes
Introduction to microprocessor notes
 
8051 data type and directives
8051 data type and directives8051 data type and directives
8051 data type and directives
 
Decimation in time and frequency
Decimation in time and frequencyDecimation in time and frequency
Decimation in time and frequency
 
RT linux
RT linuxRT linux
RT linux
 
Addressing modes of 8051
Addressing modes of 8051Addressing modes of 8051
Addressing modes of 8051
 
I o ports and timers of 8051
I o ports and timers of 8051I o ports and timers of 8051
I o ports and timers of 8051
 
Mos and cmos technology
Mos and cmos technologyMos and cmos technology
Mos and cmos technology
 
Clampers and clippers
Clampers and clippersClampers and clippers
Clampers and clippers
 
Linked lists
Linked listsLinked lists
Linked lists
 
Arrays
ArraysArrays
Arrays
 
Electro Magnetic Wave Propagation
Electro Magnetic Wave PropagationElectro Magnetic Wave Propagation
Electro Magnetic Wave Propagation
 
Satellite communications
Satellite communicationsSatellite communications
Satellite communications
 
Electronics in daily life
Electronics in daily lifeElectronics in daily life
Electronics in daily life
 
BSc I year practicals
BSc I year practicalsBSc I year practicals
BSc I year practicals
 
Combinational circuits
Combinational circuitsCombinational circuits
Combinational circuits
 
Applications of op amps
Applications of op ampsApplications of op amps
Applications of op amps
 

Graphs in c language

  • 1. Graphs in ‘C’ Language By Dr. C. Saritha Lecturer in Electronics SSBN Degree College, Anantapur
  • 2. Data Structure • Data structure is a particular way of sorting and organizing data in a computer. So that it can be used efficiently. • Different kinds of data structures are suited to different kinds of applications.
  • 3. Types of Data Structure • Linear Data Structure: A data structure is said to be linear data structure if its elements form a sequence. Ex: Array, Stack , Queue and linked list. • Non-Linear Data Structure: Elements in a non-linear data structure do not form a sequence. Ex: Tree and Graph.
  • 4. Types of data structure Data Structures Linear Non-Linear Arrays Linked lists Stack Queue Trees Graphs
  • 5. Trees • A tree is hierarchical collection of nodes. One of the nodes, known as the root, is at the top of the hierarchical. • Each node can have utmost one link coming into it. • The node where the link originates is called the parent node. The root node has no parent.
  • 6. Continue… • The links leaving a node (any number of links are allowed) point to child nodes. A B C D E F G H I J
  • 7. Graphs • A graph is a set of nodes (also called vertices) and a set of arcs (also called edges). A sample graph is as fallows: A B C D E
  • 8. Edges & Vertices • Each node in a graph is known as a vertex of the graph. • The Each edge of a graph contains two vertices.
  • 9. Types of Graphs • The Graphs can be classified into two types. They are: Undirected graphs Directed graph
  • 10. Continue… Undirected graphs: In an undirected graph the order of pair is unimportant. Hence the pairs (v1,v2)and (v2,v1) represent same edge. Directed graph: In this the order of the vertices representing an edge is important. This pair is represented as <v1,v2> where v1 is the tail and v2 is head of v2. Thus <v1,v2> and <v2,v1> represents different edges.
  • 11. Undirected graph 1 2 5 3 4 • Set of vertices={1,2,3,4,5} • Set of edges= {(1,2),(1,3),(1,4),(2,3), (2,4),(2,5),(3,4),(4,5)}
  • 12. Directed Graphs 1 2 3 • Set of vertices={1,2,3} • Set of edges= {<1,2>, <2,1>, <2,3>, <3,2>}
  • 13. Adjacent Vectors & Incident Edges • In an undirected graph if (v1,v2) is an edge in the set of edges, then the vertices v1 and v2 are said to be adjacent and the edge (v1,v2) is incident on vertices v1 and v2. • If <v1,v2> is a directed edge, then vertex v1 is said to be adjacent to v2 while v2 is adjacent from v1. • The edge <v1,v2> is incident to v1 and v2.
  • 14. Continue… • The vertex 2 in the shown undirected graph is adjacent to 1 2 vertices 1,3,4 and 5.The edges incident 5 on vertex 3 are (1,3),(2,3) and 3 4 (3,4).
  • 15. Continue… • In the shown directed graph the 1 edges incident to vertex 2 are 2 <1,2>,<2,1>,<2,3> and <3,2>. 3
  • 16. Terminology of Graphs • Weighted graph: A graph is said to be weighted graph if it’s edges have been 2 2 assigned some non- 1 negative value as 5 weight. A weighted 6 graph is known as network. 3 4 3
  • 17. Continue… • Degree: In an undirected graph, the number of edges 1 2 connected to a node is called the degree of that node. Where is in digraph, there 4 3 are two degrees for every node they are indegree and outdegree.
  • 18. Continue… • Indegree: The indegree of node is the number of edges 1 2 coming to that node. • Out degree: The out degree of node is 3 4 the number of edges going outside that node.
  • 19. Continue… • Isolated node: If any node has no edges connected with any other node then its degree will be zero (0) and it will be called as isolated node. A B C D E
  • 20. Continue… • Source: A node, which has no incoming edges, but 1 2 has outgoing edges, is called a source. 3 4
  • 21. Continue… • Connected graph: An undirected graph B A is said to be connected if there is a path from any C node of graph to D any other node. E
  • 22. Continue… • Complete graph: An undirected graph 1 will contain n(n-1)/2 1 edges is called a complete graph 2 2 where as in the case of digraph will contain n(n-1) 3 3 edges, where n is the total number of nodes in the graph.
  • 23. Continue… • Loop: An edge will be called loop or self edge if it starts and 1 2 ends on the same node. 3 4
  • 24. Breadth first search • The general idea behind a breadth first traversal A B C beginning at a starting node A is as D E following. We first examine the starting node A, and then its F G neighbors after that the neighbors of its neighbors. • A BDE CFG
  • 25. Depth first search • In Depth first search technique also we take T H A one node as starting node. Then go to the K Y N path which from starting node and visit all the nodes which are O in the path. When we reach at the last node U then we traverse another path starting THANK YOU from that node