Graphs in c language


Published on

For PG students

  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Graphs in c language

  1. 1. Graphs in ‘C’ Language By Dr. C. Saritha Lecturer in ElectronicsSSBN Degree College, Anantapur
  2. 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. 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. 4. Types of data structure Data Structures Linear Non-LinearArrays Linked lists Stack Queue Trees Graphs
  5. 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. 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. 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. 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. 9. Types of Graphs• The Graphs can be classified into two types. They are:Undirected graphsDirected graph
  10. 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. 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. 12. Directed Graphs 1 2 3• Set of vertices={1,2,3}• Set of edges= {<1,2>, <2,1>, <2,3>, <3,2>}
  13. 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. 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. 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. 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. 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. 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. 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. 20. Continue…• Source: A node, which has no incoming edges, but 1 2 has outgoing edges, is called a source. 3 4
  21. 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. 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. 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. 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. 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