34
Discrete Math Graph theory - Shortest Path & Degree

Discrete math shortest path&degree

Embed Size (px)

Citation preview

Page 1: Discrete math shortest path&degree

Discrete Math

Graph theory - Shortest Path & Degree

Page 2: Discrete math shortest path&degree

• Submitted By-Name: Syeda Jannatul Ferdous Id: 2016-02-17-002Department: CSE

Page 3: Discrete math shortest path&degree

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

Page 4: Discrete math shortest path&degree

Types of graph:

1.Weighted graph2.Un-weighted graph3.Directed graph4.Un-directed graph

Page 5: Discrete math shortest path&degree

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.

Page 6: Discrete math shortest path&degree

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.

Page 7: Discrete math shortest path&degree

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

Page 8: Discrete math shortest path&degree

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

Page 9: Discrete math shortest path&degree

S Sameen Fatima 9

Directed Graph (digraph)

In a digraph edges have directions

Graph Theory

Page 10: Discrete math shortest path&degree

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

Page 11: Discrete math shortest path&degree

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.

Page 12: Discrete math shortest path&degree

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]

Page 13: Discrete math shortest path&degree

SHORTEST PATH PROBLEM:

• Given the graph below, suppose we wish to find the shortest path from vertex 1 to vertex 13.

Page 14: Discrete math shortest path&degree

EXAMPLE:• After some consideration, we may determine that the

shortest path is as follows, with length 14

• Other paths exists, but they are longer

Page 15: Discrete math shortest path&degree

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.

Page 16: Discrete math shortest path&degree

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.

Page 17: Discrete math shortest path&degree

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

Page 18: Discrete math shortest path&degree

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

Page 19: Discrete math shortest path&degree

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.

Page 20: Discrete math shortest path&degree
Page 21: Discrete math shortest path&degree

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.

Page 22: Discrete math shortest path&degree

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.

Page 23: Discrete math shortest path&degree
Page 24: Discrete math shortest path&degree

EXAMPLE• Find the in-degree and out-degree of each vertex in the

graph G with directed edges.

24

The Directed Graph G.

Page 25: Discrete math shortest path&degree

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

Page 26: Discrete math shortest path&degree
Page 27: Discrete math shortest path&degree
Page 28: Discrete math shortest path&degree
Page 29: Discrete math shortest path&degree

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

Page 30: Discrete math shortest path&degree
Page 31: Discrete math shortest path&degree
Page 32: Discrete math shortest path&degree
Page 33: Discrete math shortest path&degree

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

Page 34: Discrete math shortest path&degree

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.