GRAPHS • Graphs are natural models that are used to represent arbitrary relationships among data objects • Graphs are used to model problems in computer science, engineering, and many other disciplines • 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 • a set of edges E • 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 • in an undirected graph, every edge is an unordered pair of vertices connected by the edge
Applications • Cities and the highways connecting them form a graph • the components on a circuit board with the connections among them also forms a graph • An organic chemical compound can be considered as graph with the atoms as the vertices and the bonds between them as edges • The people living in a city can be regarded as the vertices of a graph with the relationship is acquainted with describing the edges • People working in a corporation form a directed graph with the relation “supervises” de-scribing the edges
Udirected Graph G1 Directed Graph G2
terminology • Incident edge: is the edge entering a given node which is given as an ordered pair (Vi, Vj) • Example: find the incident edges on vertex 1 on the previous graphs • Solution the incident edges on vertex 1 for G1 are V(1,2), V(1,3) and V(1,4) The incident edges on vertex 1 for G2 are V(1,2) • Degree of vertex is the number of edges incident onto the vertex • Example, in graph G1, the degree of vertex 1 is 3, because 3 edges are incident onto it
Directed Graph • For a directed graph, the degree of vertex is described by indegree and outdegree keywords • The Indegree of a given vertex is the number of edges incident onto that vertex with the vertex as head • The Outdegree of a given vertex is the number of edges incident onto that vertex, with the vertex as the tail • Example: Find the indegree and out degree for vertex 2 in grapg G2 • Solution: The indegree of Vertex 2 is 1 while the outdegree is 2 Directed edge
Terminology • Directed edge: A directed edge between two vertices vi and vj is an ordered pair denoted by <vi,vj> • Undirected edge: An undirected edge between the vertices vi and vj is an unordered denoted by (vi,vj) • 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)
Connected graph: • A graph G is said to be connected if for every pair of distinct vertices (vi,vj), there is a path from vi to vj. A connected graph is shown Connected Grapgh
Completely connected graph • A graph G is completely connected if, for every pair of distinct vertices (vi,vj), there exists an edge. A completely connected graph is shown Completely connected grapgh
REPRESENTATIONS OF A GRAPH • A graph can be represented by one of the following ways • Array representation • Linked list representation
Array Representation • One way of representing a graph with n vertices is to use an n2 matrix • If there is an edge from vi to vj then the entry in the matrix with row index as vi and column index as vj is set to 1 • If e is the total number of edges in the graph, then there will 2e entries which will be set to 1, as long as G is an undirected graph • if G were a directed graph, only e entries would have been set to 1 in the adjacency matrix
Array Representation
Notes on Adjacency matrix for directed graph • The indegree of a given vertex in a directed graph can be found by counting the number 1s in the column below that vertex • The out degree can be found by counting the number of ones in the row corresponds to a given vertex
Linked List Representation • Another way of representing a graph G is to maintain a list for every vertex containing all vertices adjacent to that vertex, as shown in
Examples • Write a function to build the adjacency matrix for the following graph #include <iostream.h> #define MAX 10 /* a function to build an adjacency matrix of the graph*/ void buildadjm(int adj[][MAX], int n) { int i,j; for(i=0;i<n;i++) for(j=0;j<n;j++) { cout<<"Enter 1 if there is an edge from”<<i<<“,”<<j<<“ otherwise enter 0 “<<endl; cin<<adj[i][j]); } }
Write a function to compute the outdegree and another to compute the indegree of an adjacent matrix /* a function to compute outdegree of a given vertex*/ int outdegree(int adj[][MAX],int x,int n) { int i, count =0; for(i=0;i<n;i++) if( adj[x][i] ==1) count++; return count; } /* a function to compute indegree of a node*/ int indegree(int adj[][MAX],int x,int n) { int i, count =0; for(i=0;i<n;i++) if( adj[i][x] ==1) count++; return count; } Examples
Examples void main() { int adj[MAX][MAX], node,n,i; cout<<"Enter the number of nodes in graph maximum =“<<MAX<<endl; cin>>n; buildadjm(adj,n); for(i=0;i<n;i++) { cout<<"The indegree of the node “<<i<<“ is” <<indegree(adj,i,n)<<endl; cout<<"The outdegree of the node “<<i<<“is”<<outdegree(adj,i,n)<<endl; } }
DEPTH-FIRST TRAVERSAL • A graph can be traversed either by using the depth-first traversal or breadth-first traversal • In depth-first traversal, the nodes are visited in forward direction as long as possible • For example the depth first traversing for the graph shown results in one of the following sequences • 0 1 2 6 7 8 5 3 4 • 0 4 3 5 8 6 7 2 1
Example • Write a function to implement the depth first traversal /* a function to visit the nodes in a depth-first order */ void dfs(int x,int visited[],int adj[][max],int n) { int j; visited[x] = 1; cout<<"The node visited id"<<x<<endl; for(j=0;j<n;j++) if(adj[x][j] ==1 && visited[j] ==0) dfs(j,visited,adj,n); }
BREADTH-FIRST TRAVERSAL • In this algorithm a graph is traversed by visiting all the adjacent nodes/vertices of a node/vertex first • For example if the graph G is traversed using the breadth first the nodes visited will be V1, V2, V5, V4, V3, V6, V7, V8, V9
void bfs(int adj[][MAX], int x,int visited[], int n, struct node **p) { int y,j,k; *p = addqueue(*p,x); do{ *p = deleteq(*p,&y); if(visited[y] == 0) { printf("\nnode visited = %d\t",y); visited[y] = 1; for(j=0;j<n;j++) if((adj[y][j] ==1) && (visited[j] == 0)) *p = addqueue(*p,j); } }while((*p) != NULL); } void main() { int adj[MAX][MAX]; int n; struct node *start=NULL; int i, visited[MAX]; printf("enter the number of nodes in graph maximum = %d\n",MAX); scanf("%d",&n); buildadjm(adj,n); for(i=0; i<n; i++) visited[i] =0; for(i=0; i<n; i++) if(visited[i] ==0) bfs(adj,i,visited,n,&start); } Sample program
CONNECTED COMPONENT OF A GRAPH • The connected component of a graph is a maximal subgraph of a given graph, which is connected • As an example the maximal subgraph of G1 is shown to the right
Strongly Connected Component • a strongly connected component is that component of the graph in which, for every pair of distinct vertices vi and vj, there is a directed path from vi to vj
Example
DEPTH-FIRST SPANNING TREE AND BREADTH-FIRST SPANNING TREE • If graph G is connected, the edges of G can be partitioned into two disjointed sets • One is a set of tree edges, which we denote by set T • The other is a set of back edges, which we denote by B • The tree edges are precisely those edges that are followed during the depth-first traversal or during the breadth-first traversal of graph G • If we consider only the tree edges, we get a subgraph of G containing all the vertices of G, and this subgraph is a tree called spanning tree of the graph G
Example 1 spanning usind depth first algorithm • Consider the subgraph shown on the top graph • One of the possible depth-first traversal orders for this tree is 1-2-3-4 • The tree edges are (1,2), (2,3) and (3,4) • The resulting spanning tree is the one shown in the bottom graph
Example 1using depth first algorithm • One of the breadth-first traversal orders for the graph shown in the top left is 1-2-4-3 • Tree edges are (1,2), (1,4) and (4,3) • One of the possible spanning trees obtained using breadth-first traversal is the one shown in the bottom
The algorithm for obtaining the depth-first spanning tree (dfst) appears T = f; {initially set of tree nodes is empty} dfst( v : node); { if (visited[v] = false) { visited[v] = true; for every adjacent i of v do { T = T È {(v,i)} dfst(i); } } }
Notes about spanning • If a graph G is not connected, the tree edges, which are precisely those edges followed during the depth-first, traversal of the graph G, constitute the depth-first spanning forest • The depth-first spanning forest will be made of trees, each of which is one of the connected components of graph G • When a graph G is directed, the tree edges, which are precisely those edges followed during the depth-first traversal of the graph G, form a depth-first spanning forest for G • In addition to this, there are three other types of edges. These are called back edges, forward edges, and cross edges
An edge A →B is called a back edge, if B is an ancestor of A in the spanning forest • A non-tree edge that goes from a vertex to a proper descendant is called a forward edge