Upload
sweetmaina
View
220
Download
0
Embed Size (px)
Citation preview
7/28/2019 Data Structure Lecture 8 Graph[1]
1/22
Lecture-8(graph)
7/28/2019 Data Structure Lecture 8 Graph[1]
2/22
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
7/28/2019 Data Structure Lecture 8 Graph[1]
3/22
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
7/28/2019 Data Structure Lecture 8 Graph[1]
4/22
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)}
01
4
2
3
When (x,y) is an edge,
we say that x is adjacent to y, and y is
adjacent from x.
7/28/2019 Data Structure Lecture 8 Graph[1]
5/22
Some Example Applications of Graph
Finding the least congested route between two phones, given connectionsbetween switching stations.
Determining if there is a way to get from one page to another, just byfollowing links.
Finding the shortest path from one city to another.
As a traveling sales-person, finding the cheapest path that passes throughall the cities that the sales person must visit.
Determining an ordering of courses so that prerequisite courses arealways taken first.
7/28/2019 Data Structure Lecture 8 Graph[1]
6/22
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
7/28/2019 Data Structure Lecture 8 Graph[1]
7/22
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
7/28/2019 Data Structure Lecture 8 Graph[1]
8/22
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
7/28/2019 Data Structure Lecture 8 Graph[1]
9/22
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)
7/28/2019 Data Structure Lecture 8 Graph[1]
10/22
Adjacency Matrix Representation
Adjacency Matrix uses a 2-D array of dimension |V|x|V| foredges. (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.
7/28/2019 Data Structure Lecture 8 Graph[1]
11/22
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 avertex to itself).
However, entries in the matrix can be accessed directly.
7/28/2019 Data Structure Lecture 8 Graph[1]
12/22
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 thelist of the first element, and so on,
1
2
3
4
vertices
edges
2 3 Null
3 4 Null
1 3 Null2
Empty
7/28/2019 Data Structure Lecture 8 Graph[1]
13/22
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)
.
.
.
.
.
.
7/28/2019 Data Structure Lecture 8 Graph[1]
14/22
Breadth First Search Algorithm
Pseudocode: Uses FIFO Queue Q
7/28/2019 Data Structure Lecture 8 Graph[1]
15/22
DFS Algorithm
Pseudocode
DFS(s)
for each vertex u Vdo color[u] White ; not visited
time
1 ; time stampfor each vertex u Vdo if color[u]=White
then DFS-Visit(u,time)
DFS-Visit(u,time)
color[u] Gray ; in progress nodesd[u] time ; d=discover timetime time+1for each v Adj[u] do
if color[u]=White
then DFS-Visit(v,time)
color[u] Blackf[u] time time+1 ; f=finish time
7/28/2019 Data Structure Lecture 8 Graph[1]
16/22
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
wijis the weight of edge (i, j), or if there is no suchedge.
Return a matrix D, where each entry dij is d(i,j).
Could also return a predecessor matrix, P, where eachentry pij is the predecessor of j on the shortest pathfrom i.
7/28/2019 Data Structure Lecture 8 Graph[1]
17/22
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.
7/28/2019 Data Structure Lecture 8 Graph[1]
18/22
Floyd-Warshall
Two possibilities:
1. Going through the vertex kdoesnthelp the path through vertices 1...k-1 is
still the shortest. 2. There is a shorter path consisting of two
subpaths, one from ito kand one from ktoj. Each subpath passes only through
vertices numbered 1 to k-1.
j
k
i
7/28/2019 Data Structure Lecture 8 Graph[1]
19/22
8a-ShortestPathsMore
Floyd-Warshall
Thus,
(since there are no intermediate vertices.)
When k= |V|, were done.
ijk
ijk
ikk
kjk
ij ij
d d d d
d w
( ) ( ) ( ) ( )
( )
min( , )
1 1 1
0Also,
7/28/2019 Data Structure Lecture 8 Graph[1]
20/22
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.
7/28/2019 Data Structure Lecture 8 Graph[1]
21/22
Code for Floyd-Warshall
Floyd-Warshall(W)
)(
)1()1()1()(
)0(
return7
),min(6
to1fordo5
to1ifordo4
to1for3
2
verticesofnumber//][1
n
k
kj
k
ik
k
ij
k
ij
D
nj
n
nk
WD
Wrowsn
dddd
7/28/2019 Data Structure Lecture 8 Graph[1]
22/22
Example of Floyd-Warshall