Top Banner
Graphs in ‘C’ Language By Dr. C. Saritha Lecturer in Electronics SSBN Degree College, Anantapur
25

Graphs in c language

Dec 15, 2014

Download

Documents

SARITHA REDDY

For PG students
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Graphs in c language

Graphs in ‘C’ Language

ByDr. C. Saritha

Lecturer in Electronics SSBN Degree College, Anantapur

Page 2: Graphs in c language

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.

Page 3: Graphs in c language

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.

Page 4: Graphs in c language

Types of data structure

Data StructuresData Structures

Linear Non-Linear

Arrays Linked lists Stack Queue Trees Graphs

Page 5: Graphs in c language

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.

Page 6: Graphs in c language

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

Page 7: Graphs in c language

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

DC

B

E

Page 8: Graphs in c language

Edges & Vertices

• Each node in a graph is known as a vertex of the graph.

• The Each edge of a graph contains two vertices.

Page 9: Graphs in c language

Types of Graphs

• The Graphs can be classified into two types. They are:

Undirected graphsDirected graph

Page 10: Graphs in c language

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.

Page 11: Graphs in c language

Undirected graph

• 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)}

1

5

2

43

Page 12: Graphs in c language

Directed Graphs

• Set of vertices={1,2,3}• Set of edges= {<1,2>, <2,1>,

<2,3>, <3,2>}

1

2

3

Page 13: Graphs in c language

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.

Page 14: Graphs in c language

Continue…

• The vertex 2 in the shown undirected graph is adjacent to vertices 1,3,4 and 5.The edges incident on vertex 3 are (1,3),(2,3) and (3,4).

1

5

2

43

Page 15: Graphs in c language

Continue…

• In the shown directed graph the edges incident to vertex 2 are <1,2>,<2,1>,<2,3> and <3,2>.

1

2

3

Page 16: Graphs in c language

Terminology of Graphs

• Weighted graph: A graph is said to be weighted graph if it’s edges have been assigned some non-negative value as weight. A weighted graph is known as network.

1 2

43

2

45

3

63

Page 17: Graphs in c language

Continue…

• Degree: In an undirected graph, the number of edges connected to a node is called the degree of that node. Where is in digraph, there are two degrees for every node they are indegree and outdegree.

1 2

43

Page 18: Graphs in c language

Continue…

• Indegree: The indegree of node is the number of edges coming to that node.

• Out degree: The out degree of node is the number of edges going outside that node.

1 2

43

Page 19: Graphs in c language

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

DC

B

E

Page 20: Graphs in c language

Continue…

• Source: A node, which has no incoming edges, but has outgoing edges, is called a source.

1 2

43

Page 21: Graphs in c language

Continue…

• Connected graph: An undirected graph is said to be connected if there is a path from any node of graph to any other node.

A

DC

B

E

Page 22: Graphs in c language

Continue…

• Complete graph: An undirected graph will contain n(n-1)/2 edges is called a complete graph where as in the case of digraph will contain n(n-1) edges, where n is the total number of nodes in the graph.

1

2

3

1

2

3

Page 23: Graphs in c language

Continue…

• Loop: An edge will be called loop or self edge if it starts and ends on the same node.

1 2

43

Page 24: Graphs in c language

Breadth first search

• The general idea behind a breadth first traversal beginning at a starting node A is as following. We first examine the starting node A, and then its neighbors after that the neighbors of its neighbors. • A BDE CFG

A B

D E

C

GF

Page 25: Graphs in c language

Depth first search

• In Depth first search technique also we take one node as starting node. Then go to the path which from starting node and visit all the nodes which are in the path. When we reach at the last node then we traverse another path starting from that node

T

K NY

H A

U

O

THANK YOU