Docsity

Data Structures & Algorithms: Graph Theory Fundamentals, Summaries of Data Structures and Algorithms

Partial preview of the text

Download Data Structures & Algorithms: Graph Theory Fundamentals and more Summaries Data Structures and Algorithms in PDF only on Docsity!

Data Structures & Algorithm

Analysis

Muhammad Hussain Mughal

What is a Graph? A graph G = (V,E) is composed of: V: set of vertices E: set of edges connecting the vertices in V An edge e = (u,v) is a pair of vertices Example:

a b

c

d e

V= {a,b,c,d,e} E= {(a,b),(a,c), (a,d), (b,e),(c,d),(c,e), (d,e)}

Terminology: Adjacent and Incident

If (v 0 , v 1 ) is an edge in an undirected

graph,

v 0 and v 1 are adjacent The edge (v 0 , v 1 ) is incident on vertices v 0 and v 1

If <v 0 , v 1 > is an edge in a directed

graph

v 0 is adjacent to v 1 , and v 1 is adjacent from v 0 The edge <v 0 , v 1 > is incident on v 0 and v 1

The degree of a vertex is the number of

edges incident to that vertex

For directed graph,

the in-degree of a vertex v is the number of edges that have v as the head the out-degree of a vertex v is 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

e d

i n

0 1

Terminology:

Degree of a Vertex

Why? Since adjacent vertices each count the adjoining edge, it will be counted twice

Terminology: Path path: sequence of vertices v 1 ,v 2 ,.. .v k such that consecutive vertices v i and v i+ are adjacent. 3 (^3 ) 3 2 a (^) b c d e a (^) b c d e a b e d c b e d c

More Terminology simple path: no repeated vertices cycle: simple path, except that the last vertex is the same as the first vertex a (^) b c d e b e c a c d a a (^) b c d e

(i) (ii) (iii) (^) 3 (iv) (a) Some of the subgraph of G 1 0 0 1

(i) (ii) (iii) (iv) (b) Some of the subgraph of G 3

G 1

G 3

Subgraphs Examples

More… tree - connected graph without cycles forest - collection of trees tree forest tree tree tree

More Connectivity

n = #vertices

m = #edges

For a tree m = n - 1

n  5

m  4

n  5

m  3

If m < n - 1, G is not connected

Oriented (Directed) Graph

A graph where edges are directed

ADT for Graph objects: a nonempty set of vertices and a set of undirected edges, where each edge is a pair of vertices functions: for all graphGraph , v , v 1 and v 2  Vertices Graph Create()::=return an empty graph Graph InsertVertex( graph , v )::= return a graph with v inserted. v has no incident edge. Graph InsertEdge( graph , v 1 , v 2 )::= return a graph with new edge between v 1 and v 2 Graph DeleteVertex( graph , v )::= return a graph in which v and all edges incident to it are removed Graph DeleteEdge( graph , v 1 , v 2 )::=return a graph in which the edge ( v 1 , v 2 ) is removed Boolean IsEmpty( graph )::= if ( graph == empty graph ) return TRUE else return FALSE List Adjacent( graph , v )::= return a list of all vertices that are

Graph Representations

Adjacency Matrix

Adjacency Lists

Examples for Adjacency Matrix

0 1 1 1 1 0 1 1 1 1 0 1 1 1 1 0             0 1 0 1 0 0 0 1 0           0 1 1 0 0 0 0 0 1 0 0 1 0 0 0 0 1 0 0 1 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 1 0                          

G 1

G 2

G 4

symmetric

Merits of Adjacency Matrix

From the adjacency matrix, to determine

the connection of vertices is easy

The degree of a vertex is

For a digraph (= directed graph), the row

sum is the out_degree, while the column

sum is the in_degree

adj mat i j

j n

_ [ ][ ]

   0 1 ind vi A j i j n ( )  [ , ]  

0 1 outd vi A i j j n ( )  [ , ]  

0 1