Sei sulla pagina 1di 22

Lecture-8(graph)

CS 103 2
Definition of Graphs
A graph is a finite set of nodes with edges
between nodes
Formally, a graph G is a structure (V,E)
consisting of
a finite set V called the set of nodes, and
a set E that is a subset of VxV. That is, E is a set of
pairs of the form (x,y) where x and y are nodes in
V

What is a Graph?
Graphs are Generalization of Trees.

A simple graph G = (V, E) consists of a non-empty set V, whose members
are called the vertices of G, and a set E of pairs of distinct vertices from V,
called the edges of G.
Undirected Directed (Digraph) Weighted
CS 103 4
Examples of Graphs
V={0,1,2,3,4}
E={(0,1), (1,2), (0,3), (3,0), (2,2), (4,3)}

0
1
4
2
3
When (x,y) is an edge,
we say that x is adjacent to y, and y is
adjacent from x.

Some Example Applications of Graph
Finding the least congested route between two phones, given connections
between switching stations.

Determining if there is a way to get from one page to another, just by
following links.

Finding the shortest path from one city to another.

As a traveling sales-person, finding the cheapest path that passes through
all the cities that the sales person must visit.

Determining an ordering of courses so that prerequisite courses are
always taken first.
Graphs Terminologies
Adjacent Vertices: there is a connecting edge.

A Path: A sequence of adjacent vertices.

A Cycle: A path in which the last and first vertices are adjacent.

Connected graph: There is a path from any vertex to every other vertex.
Path Cycle Connected Disconnected
More Graph Terminologies
Path and cycles in a digraph: must move in the direction specified by the arrow.

Connectedness in a digraph: strong and weak.

Strongly Connected: If connected as a digraph - following the arrows.

Weakly connected: If the underlying undirected graph is connected (i.e.
ignoring the arrows).
Directed Cycle Strongly Connected Weakly Connected
Further Graph Terminologies
Emanate: an edge e = (v, w) is said to emanate from v.
A(v) denotes the set of all edges emanating from v.

Incident: an edge e = (v, w) is said to be incident to w.
I(w) denote the set of all edges incident to w.

Out-degree: number of edges emanating from v -- |A(v)|

In-degree: number of edges incident to w -- |I(w)|.
Directed Graph Undirected Graph
Graph Representations
For vertices:
an array or a linked list can be used

For edges:
Adjacency Matrix (Two-dimensional array)
Adjacency List (One-dimensional array of linked lists)
Linked List (one list only)
Adjacency Matrix Representation
Adjacency Matrix uses a 2-D array of dimension |V|x|V| for
edges. (For vertices, a 1-D array is used)

The presence or absence of an edge, (v, w) is indicated by the
entry in row v, column w of the matrix.

For an unweighted graph, boolean values could be used.

For a weighted graph, the actual weights are used.
Notes on Adjacency Matrix
For undirected graph, the adjacency matrix is always symmetric.

In a Simple Graph, all diagonal elements are zero (i.e. no edge from a
vertex to itself).

However, entries in the matrix can be accessed directly.
Adjacency List Representation
This involves representing the set of vertices adjacent to each vertex as a
list. Thus, generating a set of lists.

This can be implemented in different ways.
Our representation:
Vertices as a one dimensional array
Edges as an array of linked list (the emanating edges of vertex 1 will be in the
list of the first element, and so on,
1
2
3
4
vertices
-
-
-
-
edges
- 2 - 3 Null
- 3 - 4 Null
- 1 - 3 Null - 2
Empty
Simple List Representation
Vertices are represented as a 1-D array or a linked list

Edges are represented as one linked list
Each edge contains the information about its two vertices
1
2
3
4
vertices edges
edge(1,2)
edge(1,3)
edge(2,3)
edge(2,4)
edge(4,1)
edge(4,2)
edge(4,3)
.
.
.
.
.
.
Breadth First Search Algorithm
Pseudocode: Uses FIFO Queue Q
DFS Algorithm
Pseudocode
DFS(s)
for each vertex ueV
do color[u] White ; not visited
time 1 ; time stamp
for each vertex ueV
do if color[u]=White
then DFS-Visit(u,time)

DFS-Visit(u,time)
color[u] Gray ; in progress nodes
d[u] time ; d=discover time
time time+1
for each v eAdj[u] do
if color[u]=White
then DFS-Visit(v,time)
color[u] Black
f[u] time time+1 ; f=finish time
The Floyd-Warshall Algorithm
Represent the directed, edge-weighted
graph in adjacency-matrix form.

W= matrix of weights =
w w w
w w w
w w w
11 12 13
21 22 23
31 32 33


(
(
(
w
ij
is the weight of edge (i, j), or if there is no such
edge.
Return a matrix D, where each entry d
ij
is o(i,j).
Could also return a predecessor matrix, P, where each
entry t
ij
is the predecessor of j on the shortest path
from i.
Floyd-Warshall:
Consider intermediate vertices of a path:
i j
Say we know the length of the shortest path from i
to j whose intermediate vertices are only those with
numbers 1, 2, ..., k-1.
Floyd-Warshall
Two possibilities:
1. Going through the vertex k doesnt
help the path through vertices 1...k-1 is
still the shortest.
2. There is a shorter path consisting of two
subpaths, one from i to k and one from k to
j. Each subpath passes only through
vertices numbered 1 to k-1.
j
k
i
8a-ShortestPathsMore
Floyd-Warshall
Thus,


(since there are no intermediate vertices.)
When k = |V|, were done.


ij
k
ij
k
ik
k
kj
k
ij ij
d d d d
d
w
( ) ( ) ( ) ( )
( )
min( , ) = +
=
1 1 1
0
Also,
Floyd-Warshall
Floyd-Warshall is a dynamic programming
algorithm:
Compute and store solutions to sub-
problems. Combine those solutions to
solve larger sub-problems.
Here, the sub-problems involve finding the
shortest paths through a subset of the
vertices.
Code for Floyd-Warshall
Floyd-Warshall(W)

) (
) 1 ( ) 1 ( ) 1 ( ) (
) 0 (
return 7
) , min( 6
to 1 for do 5
to 1 i for do 4
to 1 for 3
2
vertices of number // ] [ 1
n
k
kj
k
ik
k
ij
k
ij
D
n j
n
n k
W D
W rows n
d d d d

+

Example of Floyd-Warshall

Potrebbero piacerti anche