Graphs

13
9 UNIT IV GRAPHS A graph is a non-linear data structure that is widely used in solving games and puzzles. A graph is a very convenient and natural way of representing the relationships between objects thus we represent objects by vertices and the relationship between them by edges. Why do we need graphs? Graphs find their importance in many types of applications, some examples are: In a telephone system, finding the least congested route between two phones, given connections between switching stations. On the web, determine if there is a way to get to one page from another, just by following normal links. While driving, find the shortest path from one city to another. As a traveling sales person who needs to visit a number of cities, find the shortest path that includes all the cities. Determine an ordering of courses so that you always take prerequisite courses first. Definition of Graph A graph G = (V, E) consists of a collection of two sets V and E, where V is a finite non-empty set of vertices and E is a finite non-empty set of edges. Example of Graph ACET | Prepared By AnithaPadmanaban, AP / IT

Transcript of Graphs

Page 1: Graphs

9

UNIT IV GRAPHS

A graph is a non-linear data structure that is widely used in solving games and puzzles. A graph is a

very convenient and natural way of representing the relationships between objects thus we represent

objects by vertices and the relationship between them by edges.

Why do we need graphs?

Graphs find their importance in many types of applications, some examples are:

In a telephone system, finding the least congested route between two phones, given

connections between switching stations.

On the web, determine if there is a way to get to one page from another, just by following

normal links.

While driving, find the shortest path from one city to another.

As a traveling sales person who needs to visit a number of cities, find the shortest path that

includes all the cities.

Determine an ordering of courses so that you always take prerequisite courses first.

Definition of Graph

A graph G = (V, E) consists of a collection of two sets V and E, where V is a finite non-empty set

of vertices and E is a finite non-empty set of edges.

Example of Graph

Some Basic Terminologies of Graphs

1. Directed Graph or Digraph

a. A graph G = (V, E) in which every edge is having a direction.

b. This graph is unidirectional

c. Each edge connects two vertices, called the source and target; the edge connects the source to

the target (order is significant)

ACET | Prepared By AnithaPadmanaban, AP / IT

a b

c

d e

V= {a,b,c,d,e}

E= { (a,b),(a,c),(a,d),(b,e),(c,d),(c,e),(d,e) }

Page 2: Graphs

9

Vertices (V) = { V1, V2, V3 }

Edges (E) = { (V1,V2), (V1,V3), (V2,V3)}

2. Undirected Graph

a. When the edges in a graph have no direction, the graph is called undirected b. This graph is bidirectionalc. Each edge connects two verticesd. The order of the connection is unimportant

Vertices (V) = { V1, V2,V3 }

Edges (E) = { (V1,V2), (V1,V3), (V2,V3) }

ACET | Prepared By AnithaPadmanaban, AP / IT

E(Graph2) = {(1,3) (3,1) (5,9) (9,11) (5,7)

Page 3: Graphs

9

3. Weighted Graph

A graph is said to be weighted if every edge in the graph is assigned a weight or value. It can be

directed or undirected.

Weighted Directed Graph Un weighted Directed Graph

4. Connected Graph

A graph is called connected if there exists a path from any vertex to any other vertex.

ACET | Prepared By AnithaPadmanaban, AP / IT

Page 4: Graphs

9

5. Complete Graph

A complete graph is a graph in which there is an edge between every pair of vertices. (or) A

complete graph is a graph that has the maximum number of edges for undirected graph with n

vertices, the maximum number of edges is n(n-1)/2 for directed graph with n vertices, the

maximum number of edges is n(n-1)

How many total edges in a complete graph?

Each of the n vertices is incident to n-1 edges; however, we would have counted each edge twice!

Therefore, intuitively, m = n (n -1)/2.

6. Sub graph: subset of vertices and edges forming a graph

7. Strongly Connected Graph

In a directed graph if there is a path from every vertex to every other vertex then it is said to be

strongly connected graph.

ACET | Prepared By AnithaPadmanaban, AP / IT

0 0

1 2 3

1 2 0

1 23

01 2

3G1

Page 5: Graphs

9

8. Weakly Connected Graph

In a directed graph when there are at least two vertices that are not connected is said to be Weakly

Connected Graph

9. Adjacent Vertices

Two nodes are adjacent if they are connected by an edge

10. Path

A path in a graph is a sequence of vertices that connect two nodes in a graph.

Simple Path: no repeated vertices

11. Length

The length of the path is number of edges on the path

ACET | Prepared By AnithaPadmanaban, AP / IT

5 is adjacent to 77 is adjacent from 5

Page 6: Graphs

9

12. Loop

In a graph if any vertex that connects to itself is known loop.

13. Cycle

A cycle is a simple path in which the first and the last vertices are the same

14. Acyclic Graph

A directed graph without cycle is called acyclic graphs. Also called as Directed Acyclic Graphs

(DAG).

15. Tree

An undirected graph which contains no cycles is called a forest. 

16. Forest: collection of trees

17. Degree

The degree of a vertex is the number of edges incident to that vertex.

For directed graph;

in-degree (v) : the number of edges that have v as the head

out-degree (v) : the number of edges that have v as the tail

If di is the degree of a vertex i in a graph G with n vertices and e edges, the number of edges is

ACET | Prepared By AnithaPadmanaban, AP / IT

e=(∑0

n−1

d i) /2

Page 7: Graphs

9

18. Bridge

A bridge is a single edge whose removal disconnects a graph

19. Null Graphs

A null graph contains no edges

20. Bipartite graph

A graph is said to be bipartite if the vertices can be split into sets V1 and V2. Such that there are

no edges between two vertices of V1 or vertices of V2.

Restrictions on graphs

A graph may not have an edge from a vertex, i, back to itself. Such edges are known as self loops

A graph may not have multiple occurrences of the same edge. If we remove this restriction, we

obtain a data referred to as a multigraph

ACET | Prepared By AnithaPadmanaban, AP / IT

undirected graphdegree

in-degree & out-degree0

1

2G3

in:1, out: 1

in: 1, out: 2

in: 1, out: 0

directed graph

30

1 23

33

3G1

01 2

3 4 5 6

2

3 3

1 1 1

G2

1

Page 8: Graphs

9

21. Multi Graph 22. Self Edge Graph or loops

Representation of Graphs

For graphs to be computationally useful, they have to be conveniently represented in programs

There are two computer representations of graphs:

Adjacency matrix representation (Two-dimensional Array)

Adjacency lists representation

Adjacency matrix representation

In this representation, each graph of n nodes is represented by an n x n matrix A, that is, a two-

dimensional array

Let G=(V,E) be a graph with n vertices.

The adjacency matrix of G is a two-dimensional n by n array, say adj_mat

If the edge (vi, vj) is in E(G), adj_mat[i][j]=1

If there is no such edge in E(G), adj_mat[i][j]=0

The adjacency matrix for an undirected graph is symmetric;

The adjacency matrix for a digraph need not be symmetric

Examples for Adjacency Matrix

ACET | Prepared By AnithaPadmanaban, AP / IT

Page 9: Graphs

9

Merits of Adjacency Matrix

ACET | Prepared By AnithaPadmanaban, AP / IT

Adjacency Matrix

[010100

010 ]

[01111011

1101

1110 ]

Page 10: Graphs

9 References:

ACET | Prepared By AnithaPadmanaban, AP / IT

Page 11: Graphs

9

http://www.kkhsou.in/main/EVidya2/computer_science/intro_graph.html

http://www.stoimen.com/blog/2012/08/31/computer-algorithms-graphs-and-their-representation/

http://www.siteforinfotech.com/2012/11/graph-definition.html

ACET | Prepared By AnithaPadmanaban, AP / IT