Upload
syeda-ema
View
55
Download
5
Embed Size (px)
Citation preview
Discrete Math
Graph theory - Shortest Path & Degree
• Submitted By-Name: Syeda Jannatul Ferdous Id: 2016-02-17-002Department: CSE
S Sameen Fatima 3
What Is a Graph?
• A graph, G is an ordered triple (V, E, f)
consisting of– V is a set of nodes, points, or vertices. – E is a set, whose elements are known as
edges or lines. – f is a function that maps each element
of E to an unordered pair of vertices in V.
Graph Theory
Types of graph:
1.Weighted graph2.Un-weighted graph3.Directed graph4.Un-directed graph
Types of graphs:• Weighted: A graph is a weighted graph if a number (weight) is assigned to each edge.Example: Such weights might represent, for example, costs, lengths or capacities, etc.
• Un-weighted: A graph is a un-weighted graph if a number(weight) is not assigned to each edge.
Types of graphs:• Directed: An directed graph is one in which edges have orientation.
• Un-directed: An undirected graph is one in which edges have no orientation.
Loop, Multiple edges
• Loop : An edge whose endpoints are equal
• Multiple edges : Edges have the same pair of endpoints
Graph Theory S Sameen Fatima 7
loop
Multiple edges
UNDIRECTED GRAPHSThe graph in which u and v(vertices) are endpoints of an edge of graph G is called an undirected graph G.
U V
LOOP
S Sameen Fatima 9
Directed Graph (digraph)
In a digraph edges have directions
Graph Theory
S Sameen Fatima 10
Weighted Graph
Weighted graph is a graph for which each edge has an associated weight, usually given by a weight function w: E R.
Graph Theory
1 2 3
4 5 6
.5
1.2
.2
.5
1.5.3
1
4 5 6
2 32
1 35
Graph Theory S Sameen Fatima 11
Simple Graph
Simple graph : A graph has no loops or multiple edges
loopMultiple edges
It is not simple. It is a simple graph.
S Sameen Fatima 12
Path• A path is a sequence of vertices such that there is
an edge from each vertex to its successor. • A path is simple if each vertex is distinct.• A circuit is a path in which the terminal vertex
coincides with the initial vertex
Graph Theory
1 2 3
4 5 6
Simple path: [ 1, 2, 4, 5 ]Path: [ 1, 2, 4, 5, 4]Circuit: [ 1, 2, 4, 5, 4, 1]
SHORTEST PATH PROBLEM:
• Given the graph below, suppose we wish to find the shortest path from vertex 1 to vertex 13.
EXAMPLE:• After some consideration, we may determine that the
shortest path is as follows, with length 14
• Other paths exists, but they are longer
Negative Cycles:• Clearly, if we have negative vertices, it may be
possible to end up in a cycle whereby each pass through the cycle decreases the total length
• Thus, a shortest length would be undefined for such a graph
• Consider the shortest pathfrom vertex 1 to 4...
• We will only consider non-negative weights.
Shortest Path Example:
• Given:– Weighted Directed graph G = (V, E).– Source s, destination t.
• Find shortest directed path from s to t.
s
3
t
2
6
7
45
23
18 2
9
14
15 5
30
20
44
16
11
6
19
6
Cost of path s-2-3-5-t = 9 + 23 + 2 + 16 = 48.
Problem: shortest path from a to z
a
b d f
z
c e g
45 5
7
421
553
34
a b c d e f g z S0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ax 4(a) 3(a) ∞ ∞ ∞ ∞ ∞ a,c
x x
1
5
72
3
46
20
40
1535
35
10
15
10
50
75
1 2 3 4 5 6 7 S
0 ∞ ∞ ∞ ∞ ∞ ∞ 1
x 15(1) 35(1) ∞ 20(1) ∞ ∞ 1,2
x x
Theorems
Dijkstra’s algorithm finds the length of a shortest path between two vertices in a connected simple undirected weighted graph G=(V,E).
The time required by Dijkstra's algorithm is O(|V|2).
It will be reduced to O(|E|log|V|) if heap is used to keep {vV\Si : L(v) < }, where Si is the set S after iteration i.
S Sameen Fatima 21
Degree
Degree: Number of edges incident on a node
Graph Theory
A
D E F
B C
The degree of B is 2.
S Sameen Fatima 22
Degree (Directed Graphs)• In degree: Number of edges entering a node• Out degree: Number of edges leaving a node• Degree = Indegree + Outdegree
Graph Theory
1 2
4 5
The in degree of 2 is 2 andthe out degree of 2 is 3.
EXAMPLE• Find the in-degree and out-degree of each vertex in the
graph G with directed edges.
24
The Directed Graph G.
EXAMPLEThe in-degrees in G aredeg−(a) = 2 deg−(b) = 2deg−(c) = 3 deg−(d) = 2deg−(e) = 3 deg−(f ) = 0
The out-degrees in G are deg+(a) = 4 deg+(b) = 1deg+(c) = 2 deg+(d) = 2deg+(e) = 3 deg+(f ) = 0
S Sameen Fatima 29
Degree: Simple Facts
• If G is a digraph with m edges, then indeg(v) = outdeg(v) = m = |E |
• If G is a graph with m edges, then deg(v) = 2m = 2 |E |
– Number of Odd degree Nodes is even
Graph Theory
EXAMPLE How many edges are there in a graph with 10
vertices each of degree 5?
o vV deg(v) = 6·10 = 60o 2E= vV deg(v) =60o E=30
EXAMPLE How many edges are there in a graph with 9 vertices
each of degree 5?
o vV deg(v) =5 · 9 = 45o 2E= vV deg(v) =45o 2E=45o E=22.5o Which is not possible.