0% found this document useful (0 votes)
10 views

Graphs

The document defines and provides examples of graphs and their representations. A graph consists of vertices and edges, and can be directed or undirected. Complete graphs have maximum possible edges. Adjacency matrices and lists are common representations, with matrices storing connections in a 2D array and lists storing each vertex's neighbors. Degrees represent the number of incident edges on a vertex.

Uploaded by

ad599066
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Graphs

The document defines and provides examples of graphs and their representations. A graph consists of vertices and edges, and can be directed or undirected. Complete graphs have maximum possible edges. Adjacency matrices and lists are common representations, with matrices storing connections in a 2D array and lists storing each vertex's neighbors. Degrees represent the number of incident edges on a vertex.

Uploaded by

ad599066
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 49

Graphs

Definition
 A graph G consists of two sets
– a finite, nonempty set of vertices V(G)
– a finite, possible empty set of edges E(G)
– G(V,E) represents a graph
 An undirected graph is one in which the pair of
vertices in a edge is unordered, (v 0, v1) = (v1,v0)
 A directed graph is one in which each edge is a
directed pair of vertices, <v0, v1> != <v1,v0>
tail head
Examples for Graph
0 0 0

1 2 1 2
1
3
3 4 5 6
G1 2
G2
complete graph incomplete graph G3
V(G1)={0,1,2,3} E(G1)={(0,1),(0,2),(0,3),(1,2),(1,3),(2,3)}
V(G2)={0,1,2,3,4,5,6} E(G2)={(0,1),(0,2),(1,3),(1,4),(2,5),(2,6)}
V(G3)={0,1,2} E(G3)={<0,1>,<1,0>,<1,2>}
complete undirected graph: n(n-1)/2 edges
complete directed graph: n(n-1) edges
Complete Graph
 A complete graph is a graph that has the
maximum number of edges
– for undirected graph with n vertices, the maximum
number of edges is n(n-1)/2
– for directed graph with n vertices, the maximum
number of edges is n(n-1)
– example: G1 is a complete graph
Adjacent and Incident
 If (v0, v1) is an edge in an undirected graph,
– v0 and v1 are adjacent
– The edge (v0, v1) is incident on vertices v0 and v1
 If <v0, v1> is an edge in a directed graph
– v0 is adjacent to v1, and v1 is adjacent from v0
– The edge <v0, v1> is incident on v0 and v1
Example of a graph with feedback loops and a
multigraph
0

0 2 1 3

1 2
self edge multigraph:
(a) (b) multiple occurrences
of the same edge

Figure 6.3
Subgraph and Path
 A subgraph of G is a graph G’ such that V(G’)
is a subset of V(G) and E(G’) is a subset of
E(G)
 A path from vertex vp to vertex vq in a graph G,
is a sequence of vertices, vp, vi1, vi2, ..., vin, vq,
such that (vp, vi1), (vi1, vi2), ..., (vin, vq) are edges
in an undirected graph
 The length of a path is the number of edges on
it
0 0 0 1 2 0

1 2 1 2 3 1 2
3
3
G1 (i) (ii) (iii) (iv)
(a) Some of the subgraph of G1

0 0 0 0
0
1 1 1
1
2 2

2
(i) (ii) (iii) (iv)
(b) Some of the subgraph of G3
Simple Path and Style
 A simple path is a path in which all vertices,
except possibly the first and the last, are distinct
 A cycle is a simple path in which the first and
the last vertices are the same
 In an undirected graph G, two vertices, v0 and v1,
are connected if there is a path in G from v0 to v1
 An undirected graph is connected if, for every
pair of distinct vertices vi, vj, there is a path
from vi to vj
connected

0 0

1 2 1 2
3
3 4 5 6
G1
G2
tree (acyclic graph)
Connected Component
 A connected component of an undirected graph
is a maximal connected subgraph.
 A tree is a graph that is connected and acyclic.
 A directed graph is strongly connected if there
is a directed path from vi to vj and also
from vj to vi.
 A strongly connected component is a maximal
subgraph that is strongly connected.
H1 0 H2 4

2 1 5

3 6

G4 (not connected)
not strongly connected

0
0 2

1
2
G3 strongly connected component
(maximal strongly connected subgraph)
Degree
 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
n 1

e( d )/ 2
0
i
undirected graph
degree
3 0
0 2
1 2
3 1 23 3 3
3 4 5 6
3
G13 1 1 G2 1 1
0 in:1, out: 1
directed graph
in-degree
out-degree 1 in: 1, out: 2

2 in: 1, out: 0
G3
ADT for Graph
structure Graph is
objects: a nonempty set of vertices and a set of undirected edges, where each

edge is a pair of vertices


functions: for all graph  Graph, v, v1 and v2  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, v1,v2)::= return a graph with new edge
between v1 and v2
Graph DeleteVertex(graph, v)::= return a graph in which v and all edges
incident to it are removed
Graph DeleteEdge(graph, v1, v2)::=return a graph in which the edge (v1,
v2 )
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 adjacent to v
Graph Representations
 Adjacency Matrix
 Adjacency Lists
 Adjacency Multilists
Adjacency Matrix
 Let G=(V,E) be a graph with n vertices.
 The adjacency matrix of G is a two-dimensional
n by n array, say adj_mat
 If the edge (vi, vj) is in E(G), adj_mat[i][j]=1
 If there is no such edge in E(G), adj_mat[i][j]=0
 The adjacency matrix for an undirected graph is
symmetric; the adjacency matrix for a digraph
need not be symmetric
0 0 4
0
1 2 2 1 5

3 1 3 6

7
G1 2 0 1 1 0 0 0 0 0
G2 1 0 0 1 0 0 0 0
0 1 1 1 
1 0 1 1  1 0 0 1 0 0 0 0
 0 1 0  
1 1 0 1   0 1 1 0 0 0 0 0
 1 0 1 
  0 0 0
0 0 0 0 0 1 0 0
1 1 1 0  
0 0 0 0 1 0 1 0
0 0 0 0 0 1 0 1
undirected: n2/2  
symmetric 0 0 0 0 0 0 1 0
directed: n2
G4
Merits of Adjacency Matrix
 From the adjacency matrix, to determine the
connection of vertices is easy n 1
 The degree of a vertex is  adj _ mat[i][ j ]
j 0
 For a digraph, the row sum is the out_degree,
while the column sum is the in_degree
n 1 n 1
ind (vi )   A[ j , i ] outd (vi )   A[i , j ]
j 0 j 0
Data Structures for Adjacency Lists
Each row in adjacency matrix is represented as an adjacency list.

#define MAX_VERTICES 50
typedef struct node *node_pointer;
typedef struct node {
int vertex;
struct node *link;
};
node_pointer graph[MAX_VERTICES];
int n=0; /* vertices currently in use *
0 0 4
2 1 5
1 2 3 6
3 7
0 1 2 3 0 1 2
1 0 2 3 1 0 3
2 0 1 3 2 0 3
3 0 1 2 3 1 2
G1 0 4 5
5 4 6
0 1 6 5 7
1 0 2 1
7 6
2
G3 G4
2
Some Graph Operations
 Traversal
Given G=(V,E) and vertex v, find all wV,
such that w connects v.
– Depth First Search (DFS)
preorder tree traversal
– Breadth First Search (BFS)
level order tree traversal
 Connected Components
 Spanning Trees
depth first search: v0, v1, v3, v7, v4, v5, v2, v6

breadth first search: v0, v1, v2, v3, v4, v5, v6, v7
Depth First Search
#define FALSE 0
#define TRUE 1
short int visited[MAX_VERTICES];

void dfs(int v)
{
node_pointer w;
visited[v]= TRUE;
printf(“%5d”, v);
for (w=graph[v]; w; w=w->link)
if (!visited[w->vertex])
dfs(w->vertex);
}
Breadth First Search
typedef struct queue *queue_pointer;
typedef struct queue {
int vertex;
queue_pointer link;
};
void addq(queue_pointer *,
queue_pointer *, int);
int deleteq(queue_pointer *);
Breadth First Search (Continued)
void bfs(int v)
{
node_pointer w;
queue_pointer front, rear;
front = rear = NULL;
adjacency list: O(e)
printf(“%5d”, v); adjacency matrix: O(n )
2

visited[v] = TRUE;
addq(&front, &rear, v);
while (front) {
v= deleteq(&front);
for (w=graph[v]; w; w=w->link)
if (!visited[w->vertex]) {
printf(“%5d”, w->vertex);
addq(&front, &rear, w->vertex);
visited[w->vertex] = TRUE;
}
}
}
Spanning Trees
 When graph G is connected, a depth first or
breadth first search starting at any vertex will
visit all vertices in G
 A spanning tree is any tree that consists solely
of edges in G and that includes all the vertices
 E(G): T (tree edges) + N (nontree edges)
where T: set of edges used during search
N: set of remaining edges
Examples of Spanning Tree

0 0 0 0

1 2 1 2 1 2 1 2
3 3 3 3

G1 Possible spanning trees


Spanning Trees
 Either dfs or bfs can be used to create a
spanning tree
– When dfs is used, the resulting spanning tree is
known as a depth first spanning tree
– When bfs is used, the resulting spanning tree is
known as a breadth first spanning tree
 While adding a nontree edge into any spanning
tree, this will create a cycle
Minimum Cost Spanning Tree
 The cost of a spanning tree of a weighted
undirected graph is the sum of the costs of the
edges in the spanning tree
 A minimum cost spanning tree is a spanning
tree of least cost
 Three different algorithms can be used
– Kruskal
– Prim Select n-1 edges from a weighted graph
– Sollin of n vertices with minimum cost.
Kruskal’s Idea
 Build a minimum cost spanning tree T by
adding edges to T one at a time
 Select the edges for inclusion in T in
nondecreasing order of the cost
 An edge is added to T if it does not form a
cycle
 Since G is connected and has n > 0 vertices,
exactly n-1 edges will be selected
0 10 5

2 12 3

1 14 6 0 0 0
28
1 16 2 10 1 1 10 1
14 16
3 2218 6 5 6 2 5 6 2 5 6 2
24
25
3 4 18 12 4 4
4
4 24 6 22
3 3 3

4 25 5
6/9
0 10 5

2 12 3

1 14 6

1 16 2 0 0 0

18 10 1 10 1 10 1
3 6 14 16
14

3 22 4 5 6 2 5 6 2 5 6 2
12 12 12
4 24 6 4 4 4
3 3 3
4 25 5

0 28 1 + 3 6

cycle
0 10 5

2 12 3

1 14 6
0 0
1 16 2
10 1 10 1
3 18 6 14 16 14 16

5 6 2 5 6 2
3 22 4 25
12 12
4 4
4 24 6 22
22
3 + 4 6 3
4 25 5 cycle
cost = 10 +25+22+12+16+14

0 28 1
Kruskal’s Algorithm
T= {};
while (T contains less than n-1 edges
&& E is not empty) {
choose a least cost edge (v,w) from E;
min heap construction time O(e)
delete (v,w) from E;
choose and delete O(log e)
if ((v,w) does not create a cycle in T)
add (v,w) to T
find find & union O(log e)
else discard (v,w);
} {0,5}, {1,2,3,6}, {4} + edge(3,6) X + edge(3,4) --> {0,5},{1,2,3,4,6}
if (T contains fewer than n-1 edges)
printf(“No spanning tree\n”);

O(e log e)
Prim’s Algorithm
(tree all the time vs. forest)
T={};
TV={0};
while (T contains fewer than n-1 edges)
{
let (u,v) be a least cost edge such
that u  TV and v  TV
if (there is no such edge ) break;
add v to TV;
add (u,v) to T;
}
if (T contains fewer than n-1 edges)
printf(“No spanning tree\n”);
Examples for Prim’s Algorithm
0 28
10 1
14 16

5 6 2
24
25
18
0 4
12 0 0
22
10 1 3 10 1 10 1

5 6 2 5 6 2 5 6 2
25 25
4 4 4
22
3 3 3
0 28
10 1
14 16

5 6 2
24
25
18 12
4
22
0 3 0 0
10 1 10 1 10 1
16 14 16

5 6 2 5 6 2 5 6 2
25 25 25
12 12 12
4 4 4
22 22 22
3 3 3
0 Sollin’s Algorithm
28 vertex edge
10 1 0 0 -- 10 --> 5, 0 -- 28 --> 1
14 16 1 1 -- 14 --> 6, 1-- 16 --> 2, 1 -- 28 --> 0
2 2 -- 12 --> 3, 2 -- 16 --> 1
5 6 2 3 3 -- 12 --> 2, 3 -- 18 --> 6, 3 -- 22 --> 4
24 4 4 -- 22 --> 3, 4 -- 24 --> 6, 5 -- 25 --> 5
25
18 12 5 5 -- 10 --> 0, 5 -- 25 --> 4
4 6 6 -- 14 --> 1, 6 -- 18 --> 3, 6 -- 24 --> 4
22
3
0 0 {0,5} 0
28
1 10 1 5254 0 1 10 1
14 14 16
{1,6}
5 6 2 5 6 2 5 6 2
28
1162 1 0
12
4 4 12
18 24 4
6 4
6 3 22
3 22
3 3
Single Source All Destinations

Determine the shortest paths from v0 to all the remaining vertices.

*Figure 6.29: Graph and shortest paths from v0 (p.293)


45

50 10 path length
V0 V1 V4 1) v0 v2 10
2) v0 v2 v3 25
20 10 15 20 35 30 3) v0 v2 v3 v1 45
4) v0 v4 45
15 3
V2 V3 V5

(a) (b)
Boston
Example for the Shortest Path
1500 4
Chicago
San
Denver 250
Francisco
800
1200 3 1000
1 2
New
5 York
300 1000 1400
New Orleans 900
1700
0 7 1000
Los Angeles 6 Miami
0 1 2 3 4 5 6 7
0 0
1 300 0
2 1000 800 0
3 1200 0
4 1500 0 250
5 1000 0 900 1400
6 0 1000
7 1700 0

Cost adjacency matrix


Transitive Closure
Goal: given a graph with unweighted edges, determine if there is a path
from i to j for all i and j.
(1) Require positive path (> 0) lengths. transitive closure matrix
(2) Require nonnegative path (0) lengths. reflexive transitive closure matrix
0 0 1 0 0 0
1 0 0 1 0 0

2 0 0 0 1 0
1 2 3  
0 3 4 0 0 0 0 1
4 0 0 1 0 0
(a) Digraph G (b) Adjacency matrix A for G

0 0 1 1 1 1 0 1 1 1 1 1
0 1 0
1
 0 1 1 1
 1 1 1 1
2 0 0 1 1 1 2 0 0 1 1 1
3   3  
0 0 1 1 1 0 0 1 1 1
4 cycle 4
0 0 1 1 1 0 0 1 1 1 reflexive

(c) transitive closure matrix A+ (d) reflexive transitive closure matrix A*


There is a path of length 0
Activity on Vertex (AOV) Network
definition

A directed graph in which the vertices represent tasks or activities and


the edges represent precedence relations between tasks.
predecessor (successor)

vertex i is a predecessor of vertex j iff there is a directed path from i to j.


j is a successor of i.
partial order

a precedence relation which is both transitive (i, j, k, ij & jk => ik )
and irreflexive (x xx).
acylic graph

a directed graph with no directed cycles


*Program 6.14: Topological sort (p.308)

void topsort (hdnodes graph [] , int n)


{
int i, j, k, top;
node_pointer ptr;
/* create a stack of vertices with no predecessors */
top = -1;
for (i = 0; i < n; i++)
O(n) if (!graph[i].count) {no predecessors, stack is linked through
graph[i].count = top; count field
top = i;
}
for (i = 0; i < n; i++)
if (top == -1) {
fprintf(stderr, “\n Network has a cycle. Sort terminated. \n”);
exit(1);
}
}
Continued
else {
j = top; /* unstack a vertex */
top = graph[top].count;
printf(“v%d, “, j);
for (ptr = graph [j]. link; ptr ;ptr = ptr ->link ){
/* decrease the count of the successor vertices of j */
k = ptr ->vertex;
graph[k].count --;
O(e)
if (!graph[k].count) {
/* add vertex k to the stack*/
graph[k].count = top;
top = k;
}
} O(e+n)
}
}
Activity on Edge (AOE) Networks

directed edge
– tasks or activities to be performed
vertex
– events which signal the completion of certain activities
number
– time required to perform the activity
*Figure 6.41:An AOE network(p.310)

concurrent

You might also like