Post on 03-Apr-2018
transcript
7/28/2019 Graphs KRW
http://slidepdf.com/reader/full/graphs-krw 1/21
Graph Algorithms
Kasun Ranga Wijeweera
(Email: krw19870829@gmail.com)
7/28/2019 Graphs KRW
http://slidepdf.com/reader/full/graphs-krw 2/21
What is a Graph?•
Intuitively, a graph is a collection of vertices (or nodes) andthe connections between them
• Generally, no restriction is imposed on the number of vertices
in the graph or on the number of connections one vertex can
have to other vertices
7/28/2019 Graphs KRW
http://slidepdf.com/reader/full/graphs-krw 3/21
A Simple Graph•
A simple graph G = (V, E) consists of a nonempty set V of vertices and a possibly empty set E of edges, each edge being a
set of two vertices from V
• |V| = Number of vertices
•|E| = Number of edges
7/28/2019 Graphs KRW
http://slidepdf.com/reader/full/graphs-krw 4/21
A Directed Graph•
A directed graph, or digraph, G = (V, E) consists of anonempty set V of vertices and a set E of edges (also called
arcs), where each edge is pair of vertices from V
• The difference is that one edge of a simple graph is of the
form {vi, v j}, and in this case, (vi, v j) != (v j , vi)
7/28/2019 Graphs KRW
http://slidepdf.com/reader/full/graphs-krw 5/21
A Multi Graph•
Above definitions are restrictive in that they do not allow for two vertices to have more than one edge
• A multi graph is graph in which two vertices can be joined by
multiple edges
•Formal Definition: A multi graph G = (V, E, f) is composed of a set of vertices V, a set of edges E, and a function
f: E{(vi, v j): (vi, v j in V) and (vi != v j)}
7/28/2019 Graphs KRW
http://slidepdf.com/reader/full/graphs-krw 6/21
A Pseudo Graph•
A pseudo graph is a multi graph with the condition vi != v j removed, which allows for loops to occur
• In a pseudo graph, a vertex can be joined with itself by an edge
7/28/2019 Graphs KRW
http://slidepdf.com/reader/full/graphs-krw 7/21
A Path•
A path from v1 to vn is a sequence of edges edge(v1v2),edge(v2v3), . . . , edge(vn-1vn) and is denoted as path v1, v2, v3, .
. . , vn-1, vn
• If v1 = v2 and no edge is repeated, then the path is called a
circuit• If all vertices in a circuit are different, then it is called a cycle
7/28/2019 Graphs KRW
http://slidepdf.com/reader/full/graphs-krw 8/21
A Weighted Graph•
A graph is called a weighted graph if each edge has anassigned number
• Depending on the context in which such graphs are used, the
number assigned to an edge is called it weight, cost, distance,
length, or some other name
7/28/2019 Graphs KRW
http://slidepdf.com/reader/full/graphs-krw 9/21
7/28/2019 Graphs KRW
http://slidepdf.com/reader/full/graphs-krw 10/21
A Sub Graph•
A sub graph G’ of graph G = (V, E) is a graph (V’, E’) suchthat V’ is a subset of V and E’ is a subset of E
• A sub graph induced by vertices V’ is a graph (V’, E’) such
that and edge e in E if e in E’
7/28/2019 Graphs KRW
http://slidepdf.com/reader/full/graphs-krw 11/21
Adjacent? Incident?•
Two vertices vi and v j are called adjacent if the edge(viv j) is inE
• Such an edge is called incident with the vertices vi and v j
7/28/2019 Graphs KRW
http://slidepdf.com/reader/full/graphs-krw 12/21
The Degree of a Vertex•
The degree of a vertex v, deg(v), is the number of edgesincident with v
• If deg(v) = 0, then v is called an isolated vertex
7/28/2019 Graphs KRW
http://slidepdf.com/reader/full/graphs-krw 13/21
Adjacency Matrix•
An adjacency matrix of graph G = (V, E) is a binary |V|*|V|matrix such that each entry of this matrix
a ij
1; if there exists an edge(viv j)
0; otherwise
7/28/2019 Graphs KRW
http://slidepdf.com/reader/full/graphs-krw 14/21
7/28/2019 Graphs KRW
http://slidepdf.com/reader/full/graphs-krw 15/21
Graph Traversals•
Traversing a graph consists of visiting each vertex only onetime
• Graphs may include cycles that can cause infinite loops
• To prevent infinite loops each visited vertex can be marked to
avoid revisiting it• Graphs can have isolated vertices
• To visit those isolated vertices special mechanisms are needed
• The Depth First Search algorithm is a well known algorithm
for traversing graphs
7/28/2019 Graphs KRW
http://slidepdf.com/reader/full/graphs-krw 16/21
Depth First Search Algorithm•
Each vertex v is visited and then each unvisited vertexadjacent to v is visited
• If a vertex v has no adjacent vertices or all of its adjacent
vertices have been visited, we backtrack to the predecessor of
v
• The traversal is finished if this visiting and backtracking
process leads to the first vertex where the traversal started
• If there is still some unvisited vertices in the graph, the
traversal continues restarting for one of the unvisited vertices• The algorithm assigns a unique number to each accessed
vertex so that vertices are now renumbered
7/28/2019 Graphs KRW
http://slidepdf.com/reader/full/graphs-krw 17/21
Depth First Search Algorithm
depthFirstSearch()for all vertices v
num(v) = 0;
edges = null;
i = 1;
while there is a vertex v such that num(v) is 0
DFS(v);
output edges;
7/28/2019 Graphs KRW
http://slidepdf.com/reader/full/graphs-krw 18/21
Depth First Search Algorithm
DFS(v)num(v) = i++;
for all vertices u adjacent to v
if num(u) is 0
attach edge(uv) to edges;
DFS(u);
7/28/2019 Graphs KRW
http://slidepdf.com/reader/full/graphs-krw 19/21
Reference
7/28/2019 Graphs KRW
http://slidepdf.com/reader/full/graphs-krw 20/21
Any Questions?
7/28/2019 Graphs KRW
http://slidepdf.com/reader/full/graphs-krw 21/21
Thank You!