Vous êtes sur la page 1sur 10

ACET | Prepared By AnithaPadmanaban, AP / IT

1


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

2





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 bidirectional
c. Each edge connects two vertices
d. 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

3




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

4


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

5


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




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).
5 is adjacent to 7
7 is adjacent from 5

ACET | Prepared By AnithaPadmanaban, AP / IT

6


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


2 / ) (
1
0

=
n
i
d e

ACET | Prepared By AnithaPadmanaban, AP / IT

7


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

21. Multi Graph 22. Self Edge Graph or loops





ACET | Prepared By AnithaPadmanaban, AP / IT

8


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



(
(
(
(

0
1
1
1
1
0
1
1
1
1
0
1
1
1
1
0
Adjacency Matrix
(
(
(

0
1
0
0
0
1
0
1
0

ACET | Prepared By AnithaPadmanaban, AP / IT

9


Merits of Adjacency Matrix

































ACET | Prepared By AnithaPadmanaban, AP / IT

10















References:



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

Vous aimerez peut-être aussi