Chapter 22: Graphs

 < Day Day Up >

Chapter 22: Graphs

GRAPHS

Introduction

Graphs are natural models that are used to represent arbitrary relationships among data objects. We often need to represent such arbitrary relationships among the data objects while dealing with problems in computer science, engineering, and many other disciplines. Therefore, the study of graphs as one of the basic data structures is important.

Basic Definitions and Terminology

A graph is a structure made of two components : a set of vertices V, and a set of edges E. Therefore, a graph is G = (V,E), where G is a graph. The graph may be directed or undirected. In a directed graph , every edge of the graph is an ordered pair of vertices connected by the edge, whereas in an undirected graph , every edge is an unordered pair of vertices connected by the edge. Figure 22.1 shows an undirected and a directed graph.

Incident edge: (v i ,v j ) is an edge, then edge(v i ,v j ) is said to be incident to vertices v i and v j . For example, in graph G 1 shown in Figure 22.1, the edges incident on vertex 1 are (1,2), (1,4), and (1,3), whereas in G 2 , the edges incident on vertex 1 are (1,2).

Degree of vertex: The number of edges incident onto the vertex. For example, in graph G 1 , the degree of vertex 1 is 3, because 3 edges are incident onto it. For a directed graph, we need to define indegree and outdegree. Indegree of a vertex vi is the number of edges incident onto vi, with vi as the head. Outdegree of vertex vi is the number of edges incident onto vi, with vi as the tail. For graph G 2 , the indegree of vertex 2 is 1, whereas the outdegree of vertex 2 is 2.

Directed edge: A directed edge between the vertices vi and vj is an ordered pair. It is denoted by <vi,vj>.

Undirected edge: An undirected edge between the vertices v i and v j is an unordered pair. It is denoted by (v i ,v j ).

Path : A path between vertices v p and v q is a sequence of vertices v p , v i1 , v i2 , , v in ,v q such that there exists a sequence of edges (v p , v i1 ), (v i1 , v i2 ), , ( v in , v q ). In case of a directed graph, a path between the vertices v p and v q is a sequence of vertices v p , v i1 , v i2 , , v in , v q such that there exists a sequence of edges <v p , v i1 >, < v i1 , v i2 >, , <v in , v q >. If there exists a path from vertex v p to v q in an undirected graph, then there always exists a path from v q to v p also. But, in the case of a directed graph, if there exists a path from vertex v p to v q , then it does not necessarily imply that there exists a path from v q to v p also.

Simple path: A simple path is a path given by a sequence of vertices in which all vertices are distinct except the first and the last vertices. If the first and the last vertices are same, the path will be a cycle.

Maximum number of edges: The maximum number of edges in an undirected graph with n vertices is n ( n 1)/2. In a directed graph, it is n ( n 1).

Subgraph : A subgraph of a graph G = (V,E) is a graph G where V(G) is a subset of V(G). E(G) consists of edges (v1,v2) in E(G), such that both v1 and v2 are in V(G). [Note: If G = (V,E) is a graph, then V(G) is a set of vertices of G and E(G) is a set of edges of G.]

If E(G) consists of all edges (v1,v2) in E(G), such that both v1 and v2 are in V(G), then G is called an induced subgraph of G. For example, the graph shown in Figure 22.2 is a subgraph of the graph G2.

For the graph shown in Figure 22.3, one of the induced subgraphs is shown in Figure 22.4.

In the undirected graph G, the two vertices v 1 and v 2 are said to be connected if there exists a path in G from v 1 to v 2 (being an undirected graph, there exists a path from v 2 to v 1 also).

Connected graph: A graph G is said to be connected if for every pair of distinct vertices (v i ,v j ), there is a path from v i to v j . A connected graph is shown in Figure 22.5.

Completely connected graph: A graph G is completely connected if, for every pair of distinct vertices (v i ,v j ), there exists an edge. A completely connected graph is shown in Figure 22.6.

 < Day Day Up >