graphs

42
Some Basic Terms Definition of Graph: A graph, G=(V,E), consists of two sets V and E. V is a finite non-empty set of vertices also called nodes or points. E is a set of pairs of vertices called edges also called links or arcs. V(G) and E(G) represent the sets of vertices and edges of graph G.

Upload: ajit-more

Post on 27-Sep-2015

219 views

Category:

Documents


0 download

DESCRIPTION

Basic Definitions about Graph

TRANSCRIPT

E-Learning

Some Basic TermsDefinition of Graph: A graph, G=(V,E), consists of two sets V and E. V is a finite non-empty set of vertices also called nodes or points. E is a set of pairs of vertices called edges also called links or arcs. V(G) and E(G) represent the sets of vertices and edges of graph G.Some Basic Terms (Contd.)Undirected Graph: In an undirected graph the pair of vertices representing any edge is unordered i.e. the pairs (v1,v2) and (v2,v1) represent the same edge.The edges have no direction in undirected graph.Directed Graph: In a directed graph each edge is represented by a directed pair v1 is the tail and v2 the head of the edgeThe pairs and are different edges. The edges have direction in directed graph.1234G1V(G1)={1,2,3,4}E(G1)={(1,2),(1,3),(1,4),(2,3),(2,4),(3,4)}1234567G2V(G2)={1,2,3,4,5,6,7} E(G2)={(1,2),(1,3),(2,4),(2,5),(3,6),(3,7)}123G3V(G3)={1,2,3}E(G3)={,,}The graphs G1 and G2 are undirected and G3 is directed graph.MultigraphIf the graph contains self loop or have multiple occurrences of the same edge then it is called as multigraph.

Note The maximum number of distinct ordered pairs with vivj, in a directed graph with n vertices is n(n-1).The maximum number of distinct unordered pairs (vi,vj) with vivj, in a undirected graph with n vertices is n(n-1)/2.Complete Graph: An n vertex undirected graph with exactly n(n-1)/2 edges is said to be complete.

G1 is complete graph while G2 and G3 is not complete.

Incident edges and adjacent vertices:If (v1,v2) is an edge in E(G), then we shall say the vertices v1 and v2 are adjacent and that the edge (v1,v2) is incident on vertices v1 and v2.

e.g. The vertices adjacent to vertex 2 in G2 are 4,5 and 1. The edges incident on vertex 3 in G2 are (1,3), (3,6) and (3,7).If is a directed edge, then vertex v1, will be said to be adjacent to v2 while v2 is adjacent from v1. The edge is incident to v1 and v2. e.g. In G3 the edges incident to vertex 2 are , and .

Subgraph: A subgraph of G is a graph G such that V(G)V(G) and E(G)E(G).e.g. Some of the subgraphs of G1 are shown below:1232341234

Path A path from vertex vp to vertex vq in graph G is a sequence of vertices vp, v1, v2, vn, vq such that (vp,v1), (v1, v2), , (vn, vq) are edges in E(G). If G is directed then the path consists of , , , edges in E(G).Length: The length of a path is the number of edges on it.Simple Path A simple path is a path in which all vertices except possibly the first and last are distinct. e.g. The path 1,2,4,3 is simple path whereas 1,2,4,2 is not simple path. The length of both the paths is 3.Cycle A cycle is a simple path in which the first and last vertices are the same.e.g. 1,2,3,1 is a cycle in G1 whereas 1,2,1 is a cycle in G3.

Connected verticesIn an undirected graph, G, two vertices v1 and v2 are said to be connected if there is a path in G from v1 to v2.e.g. In graph G2, vertices 2 and 7 are said to be connected because there is a path from 2 to 7.

Connected Graph: An undirected graph is said to be connected if for every pair of distinct vertices vi, vj in V(G) there is a path from vi to vj in G.

e.g. Graphs G1 and G2 are connected while G4 shown below is not. connected.12345768H1H2G4

Connected ComponentA connected component or simply a component of an undirected graph is a maximal connected sub-graph.

e.g. G4 has two components H1 and H2.

TreeA tree is a connected acyclic graph.

e.g. Graph G2 is a connected acyclic graph therefore it is a tree.

Strongly Connected Graph A directed graph G is said to be strongly connected if for every pair of distinct vertices vi, vj in V(G) there is directed path from vi to vj and also from vj to vi.

e.g. The graph G3 is not strongly connected graph because there is no path from v3 to v2 , whereas the graph G5 is strongly connected graph.

G5Strongly Connected Component: It is a maximal sub-graph of a directed graph that is strongly connected. e.g. G3 has two strongly connected components as shown below.

123Two strongly connected components of G3Degree of VertexThe degree of a vertex is the number of edges incident to that vertex.

e.g. The degree of vertex 1 in G1 is 3.

In-degree of Vertex: If G is a directed graph we define in-degree of a vertex v to be the number of edges for which v is the head.That is in-degree of a vertex v is number of incoming edges to v.Out-degree of VertexThe out-degree is defined to be the number of edges for which v is the tail. That is out-degree of a vertex v is number of out-going edges from v.

Degree of undirected graph1234567G22331111In-degree and out-degree of directed graphG131234333123in:1, out: 1in: 1, out: 2in: 1, out: 0G3

Find in-degree and out-degree of each vertex in the given graph:VertexIn-degreeOut-degreev1v2v3v4v5v6111510232020Note: If di is the degree of vertex i in a graph G with n vertices and e edges, then

Isolated vertexAn isolated vertex is a vertex with degree zero; that is, a vertex that is not an endpoint of any edge.

e.g. In graph G6 vertex d is an isolated vertex because its a vertex with degree 0.

G6Leaf vertexA leaf vertex (also called pendant vertex) is a vertex with degree one.

e.g. In graph G7, vertex 6 is an pendent vertex because it is with degree one.

G7Source and Sink vertexIn a directed graph a source vertex is a vertex with in-degree zero, while a sink vertex is a vertex with out-degree zero. e.g. In the following directed graph G8, the source and sink is indicated.

G8SourceSinkWeighted graphA graph is aweighted graphif a number (weight) is assigned to each edge i.e. each edge is labeled with some weight.Such weights might represent, for example, costs, lengths or capacities, etc. depending on the problem at hand. Such a graph is also called a network.

e.g. Graph G9 is weighted directed and G10 is weighted undirected graph.

G9

G10Graph RepresentationsAdjacency MatrixAdjacency ListsAdjacency Multilists

Adjacency MatrixLet G = (V, E) with n vertices, n1. The adjacency matrix of G is a 2-dimensional n n matrix, A such that: A[i, j]=1 iff (vi, vj)E(G) (vi, vj for a diagraph)A[i, j]=0 otherwise.The adjacency matrix for an undirected graph is symmetric; the adjacency matrix for a digraph need not be symmetric

Adjacency matrix of graph G1, G3 & G4 respectively.

Symmetric

Adjacency Matrix Space RequirementThe space needed to represent a graph using adjacency matrix is n2 words for directed graph.The space needed to represent a graph using adjacency matrix for undirected graph can half the space needed for directed graph by storing only the upper or lower triangle of the matrix.Degree calculation using adjacency matrixFor undirected graph the degree of a vertex is:

For a digraph, the row sum is the out degree, while the column sum is the in degree.

Adjacency ListsAdjacency list is a linked representation of a graph.There is one list for each vertex in the graph.Thus for n vertices there will be n linked lists.Each row in adjacency matrix is represented as an adjacency list.Each node in the linked list of vertex i consists of:Vertex number to which vertex i is adjacent.Pointer to next node in the list.The address of each linked lists first node (head node) is kept in a separate array. This array of head node addresses represents the graph.

Structure definition#define MAX_VERTICES 50typedef struct node { int vertex; struct node *link;}NODE;NODE * graph[MAX_VERTICES];Example

2341341241231234213123An undirected graph with n vertices and e edges n head nodes and 2e list nodes.Example

2314142365768712345678Degree calculation using adjacency listThe degree of any vertex in an undirected graph is the number of nodes in its adjacency list.In case of diagraph the out-degree of any vertex is the number of nodes in its adjacency list.To find in-degree of any vertex we are suppose to maintain another list called inverse adjacency list. This set of lists, will also contain one list for each vertex. The vertex i list will contain a node for each vertex to which i is adjacent.Inverse adjacency list for G3 1

2

32 NULL 1 NULL 2 NULL

Orthogonal ListIn certain applications, we require the in-degree as well as out-degree of a vertex. Hence, instead of maintaining two separate lists, we can use only one graph representation which can give us both. This is called an orthogonal list.The node structure of an orthogonal list for any directed edge :

vivjLink of viLink of vjPoints to the node where vi is the tailPoints to the node where vj is the headOrthogonal representation for graph G3

1Tail2X31Head2312XX21X23XXAdjacency MultilistsIn the adjacency list representation of an undirected graph each edge (vi,vj) is represented by two entries, one on the list for vi and the other on the list for vj.Adjacency MultilistsFor each edge there will be exactly one node, but this node will be in two lists, i.e., the adjacency lists for each of the two nodes it is incident to. The node structure is as below:

Lists in which nodes may be shared among several lists. (an edge is shared by two different paths)

marked vertex1 vertex2 path1 path2Pointer to the edge where edge is incident on vertex 1Pointer to the edge where edge is incident on vertex 2one bit mark field that may be used to indicate whether or not the edge has been examined.Adjacency Multlists for graph G1

The lists are: vertex 1: N1 N2 N3vertex 2: N1 N4 N5 vertex 3: N2 N4 N6 vertex 4: N3 N5 N6 Data Structures for Adjacency Multiliststypedef struct edge { short int marked; int vertex1, vertex2; struct edge *path1, *path2;}EDGE_POINTER;EDGE_POINTER *graph[MAX_VERTICES];