Objectives Introduction Definitions and Related Concepts

J.E.D.I 6 Graphs

6.1 Objectives

At the end of the lesson, the student should be able to: • Explain the basic concepts and definitions on graphs • Discuss the methods of graph reprentation: adjacency matrix and adjacency list • Traverse graphs using the algorithms depth-first search and breadth-first search • Get the minimum cost spanning tree for undirected graphs using Prims algorithm and Kruskals algorithms • Solve the single-source shortest path problem using Dijkstras algorithm • Solve the all-pairs shortest path problem using Floyds algorithm

6.2 Introduction

This lesson covers the nomenclature for the ADT graph. It also discusses different ways to represent a graph. The two graph traversal algorithms are also covered, as well as the minimum cost spanning tree problem and shortest path problems.

6.3 Definitions and Related Concepts

A graph, G = V, E , consists of a finite, non-empty set of vertices or nodes, V, and a set of edges, E. The following are examples of graph: Figure 1.77 Examples of Graph Data Structures 99 J.E.D.I An undirected graph is a graph in which the pair of vertices representing an edge is unordered, i.e., i,j and j,i represent the same edge. Figure 1.78 Undirected Graph Two vertices i and j are adjacent if i, j is an edge in E. The edge i, j is said to be incident on vertices i and j. Figure 1.79 Edge i, j Data Structures 100 J.E.D.I A complete undirected graph is a graph in which an edge connects every pair of vertices. If a complete undirected graph has n vertices, there are nn -12 edges in it. Figure 1.80 Complete Undirected Graph A directed graph or digraph is a graph in which each edge is represented by an ordered pair i, j, where i is the head and j is the tail of the edge. The edges i,j and j,i are two distinct edges. Figure 1.81 Directed Graph A complete directed graph is a graph in which every pair of vertices i and j are connected by two edges i,j and j,i. There are nn-1 edges in it. Figure 1.82 Complete Directed Graph Data Structures 101 J.E.D.I A subgraph of an undirected directed graph G = V,E is an undirected directed graph G’ = V’,E’ such that V ⊆ V and E’ ⊆ E. Figure 1.83 Examples of Subgraph Figure 1.84 A path from vertex u to vertex w in an undirected [directed] graph G = V,E is a sequence of vertices v o , v 1 , v 2 , ..., v m-1 , v m where v o ≡ u and v m ≡ w, such that v ,v 1 ,v 1 ,v 2 ,..., v m-1 , v m [ v ,v 1 , v 1 ,v 2 , ..., v m-1 , v m ] are edges in E. The length of a path refers to the number of edges in it. A simple path is a path in which all vertices are distinct, except possibly the first and the last. A simple path is a simple cycle if it has the same start and end vertex. Two vertices i and j are connected if there is a path from vertex i to vertex j. If for every pair of distinct vertices i and j there is a directed path to and from both vertices, it is said to be strongly connected. A maximal connected subgraph of an undirected graph is known as connected component in an undirected graph. In a directed graph G, strongly connected component refers to the maximal strongly connected component in G. A weighted graph is a a graph with weights or costs assigned to its edges. Data Structures 102 J.E.D.I Figure 1.85 Weighted Graph Figure 1.86 A spanning tree is a subgraph that connects all the vertices of a graph. The cost of a spanning tree, if it is weighted, is the sum of the weights of the branches edges in the spanning tree. A spanning tree that has the minimum cost is referred to as minimum cost spanning tree. This is not necessarily unique for a given graph. Figure 1.87 Spanning Tree

6.4 Graph Representations