0% found this document useful (0 votes)
4 views

Greedy_Algorithms_notes

The document outlines a lecture plan focused on Greedy Algorithms and Graph Theory, covering topics such as Minimum Spanning Trees, Shortest Path algorithms, and various types of graphs including trees, multigraphs, and pseudographs. It details algorithms like Prim's and Kruskal's for constructing Minimum Spanning Trees, as well as Dijkstra's and Bellman-Ford for shortest paths. The document emphasizes the importance of graph theory in computer science applications, such as navigation systems and database efficiency.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Greedy_Algorithms_notes

The document outlines a lecture plan focused on Greedy Algorithms and Graph Theory, covering topics such as Minimum Spanning Trees, Shortest Path algorithms, and various types of graphs including trees, multigraphs, and pseudographs. It details algorithms like Prim's and Kruskal's for constructing Minimum Spanning Trees, as well as Dijkstra's and Bellman-Ford for shortest paths. The document emphasizes the importance of graph theory in computer science applications, such as navigation systems and database efficiency.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 116

Greedy Algorithms

Lecture Plan (Sec. 4):


Greedy Algorithms: General strategy of greedy algorithm paradigm, Fractional knapsack,
Minimum Spanning Tree (Prims and Kruskal), Shortest Path (Dijkstra’s Algorithm), Activity
Selection, Optimal Merge Patterns
Lecture Plan (Sec. 5):
Shortest Path (Bellman-Ford Algorithm, Floyd-Warshall Algorithm)
Graphs

4
Agenda –Graphs
• Graph basics and definitions
• Vertices/nodes, edges, adjacency, incidence
• Degree, in-degree, out-degree
• Degree, in-degree, out-degree
• Subgraphs, unions, isomorphism
• Adjacency matrices
• Types of Graphs
• Trees
• Undirected graphs
• Simple graphs, Multigraphs, Pseudographs
• Digraphs, Directed multigraph
• Bipartite
• Complete graphs, cycles, wheels, cubes, complete bipartite

5
Uses of Graph Theory in CS
• Car navigation system
• Efficient database
• Build a bot to retrieve info off WWW
• Representing computational models
Many other applications.
This course we focus more on the properties of algorithmic of
graphs

6
Graphs –Intuitive Notion
A graph is a bunch of vertices (or nodes)
represented by circles which are connected by
edges, represented by line segments .
Mathematically, graphs are binary-relations on
their vertex set (except for multigraphs).
In Data Structures one often starts with trees and
generalizes to graphs. In this course, opposite
approach: We start with graphs and restrict to get
trees.

7
Trees
A very important type of graph in CS is called a tree:

Real
Tree

8
Trees
A very important type of graph in CS is called a tree:

Real
Tree
transformation

9
Trees
A very important type of graph in CS is called a tree:

Real
Tree
transformation

10
Trees
A very important type of graph in CS is called a tree:

Real Abstract
Tree Tree
transformation

11
Simple Graphs
Different purposes require different types of
graphs.
EG: Suppose a local computer network
• Is bidirectional (undirected)
• Has no loops (no “self-communication”)
• Has unique connections between computers
Sensible to represent as follows:

12
Simple Graphs
{1,2}
1 2
{1,3} {2,3} {2,4}
{3,4}
3 4
{1,4}
• Vertices are labeled to associate with particular computers
• Each edge can be viewed as the set of its two endpoints

13
Simple Graphs
DEF: A simple graph G = (V,E ) consists of a non-empty set V of
vertices (or nodes) and a set E (possibly empty) of edges where
each edge is a subset of V with cardinality 2 (an unordered pair).
Q: For a set V with n elements, how many possible edges there?

14
Simple Graphs
A: The number of pairs in V
= C (n,2) = n · (n -1) / 2
Q: How many possible graphs are there for the same set of vertices
V?

15
Simple Graphs
A: The number of subsets in the set of possible edges. There are n ·
(n -1) / 2 possible edges, therefore the number of graphs on V is
2n(n -1)/2

16
Multigraphs
If computers are connected via internet instead of directly, there
may be several routes to choose from for each connection.
Depending on traffic, one route could be better than another.
Makes sense to allow multiple edges, but still no self-loops:

17
Multigraphs
e1
1 e2 2
e3 e4 e5
3 e6 4

Edge-labels distinguish between edges sharing


same endpoints. Labeling can be thought of as
function:
e1 → {1,2}, e2 → {1,2}, e3 → {1,3}, e4 → {2,3}, e5
→ {2,3}, e6 → {1,2}
18
Multigraphs
DEF: A multigraph G = (V,E,f ) consists of a non-empty set V of
vertices (or nodes), a set E (possibly empty) of edges and a
function f with domain E and codomain the set of pairs in V.

19
Pseudographs
If self-loops are allowed we get a pseudograph:

e1 e6
1 e2 2
e3 e4 e5 e7
Now edges may be associated with a single vertex,
when the edge is a 3loop 4
e1 → {1,2}, e2 → {1,2}, e3 → {1,3},
e4 → {2,3}, e5 → {2}, e6 → {2}, e7 → {4}

20
Multigraphs
DEF: A pseudograph G = (V,E,f ) consists of a non-empty set V of
vertices (or nodes), a set E (possibly empty) of edges and a
function f with domain E and codomain the set of pairs and
singletons in V.

21
Undirected Graphs
Terminology
Vertices are adjacent if they are the endpoints of
the same edge.
e1
1 e2 2
e3 e4 e5
3 e6 4
Q: Which vertices are adjacent to 1? How about
adjacent to 2, 3, and 4?

22
Undirected Graphs
Terminology
e1
1 e2 2
e3 e4 e5
3 e6 4

A: 1 is adjacent to 2 and 3
2 is adjacent to 1 and 3
3 is adjacent to 1 and 2
4 is not adjacent to any vertex

23
Undirected Graphs
Terminology
A vertex is incident with an edge (and the edge is
incident with the vertex) if it is the endpoint of
the edge.

e1
1 e2 2
e3 e4 e5
3 e6 4
Q: Which edges are incident to 1? How about
incident to 2, 3, and 4?
24
Undirected Graphs
Terminology
e1
1 e2 2
e3 e4 e5
3 e6 4

A: e1, e2, e3, e6 are incident with 2


2 is incident with e1, e2, e4, e5, e6
3 is incident with e3, e4, e5
4 is not incident with any edge

25
Digraphs
Last time introduced digraphs as a way of
representing relations:

1 3

Q: What type of pair should each edge be (multiple


edges not allowed)?
26
Digraphs
A: Each edge is directed so an ordered pair (or tuple)
rather than unordered pair.
(2,2)
2
(1,2) (2,3)

(1,1) 1 (1,3) 3 (3,3)


(2,4) (3,4)

Thus the set of edges E is4just(4,4)


the represented
relation on V.

27
Digraphs
DEF: A directed graph (or digraph) G
= (V,E ) consists of a non-empty set V of vertices
(or nodes) and a set E of edges with E V V.
The edge (a,b) is also denoted by a →b and a is
called the source of the edge while b is called the
target of the edge.
Q: For a set V with n elements, how many possible
digraphs are there?

28
Digraphs
A: The same as the number of relations on V, which is the number
of subsets of V V so 2n·n.

29
Directed Multigraphs
If also want to allow multiple edges in a digraph,
get a directed multigraph (or multi-digraph).

Q: How to use sets and functions to deal with


multiple directed 1edges, loops? 3

30
Directed Multigraphs
A: Have function with domain the edge set and
codomain V V .
e3
2
e1 e4 e6
e2 e5
1 3
e
e →(1,2), e →(1,2), e →(2,2), e 7→ (2,3),
1 2 3 4
e5 → (2,3), e6 → (3,3), e7 → (3,3)

31
Degree
The degree of a vertex counts the number of
edges that seem to be sticking out if you looked
under a magnifying glass:

e1 e6
1 e2 2 e5
e3 e4
3

32
Degree
The degree of a vertex counts the number of
edges that seem to be sticking out if you looked
under a magnifying glass:

e1 e6
1 e2 2 e5 magnify
e3 e4
3

33
Degree
The degree of a vertex counts the number of
edges that seem to be sticking out if you looked
under a magnifying glass:

e1 e6
1 e2 2 e5 magnify
e3 e4
Thus deg(2) = 7 even though 2 only incident with 5
3
edges.
Q: How to define this formally?
34
Degree
A: Add 1 for every regular edge incident with
vertex and 2 for every loop. Thus deg(2) = 1 + 1 +
1+2+2=7

e1 e6
1 e2 2 e5 magnify
e3 e4
3

35
Oriented Degree
when Edges Directed
The in-degree of a vertex (deg-) counts the
number of edges that stick in to the vertex. The
out-degree (deg+) counts the number sticking
out.

1 3
Q: What are in-degrees and out-degrees of all the
vertices?
36
Prims and Kruskal Algorithms
(Minimal Spanning Tree
Prim’s Algorithm for Minimum Spanning Tree (MST)

Prim’s Algorithm

Step 1: Determine an arbitrary vertex as the starting vertex of the MST.


Step 2: Follow steps 3 to 5 till there are vertices that are not included in the
MST (known as fringe vertex).
Step 3: Find edges connecting any tree vertex with the fringe vertices.
Step 4: Find the minimum among these edges.
Step 5: Add the chosen edge to the MST if it does not form any cycle.
Step 6: Return the MST and exit.ny tree vertex with the fringe vertices.
Step 4: Find the minimum among these edges.
Step 5: Add the chosen edge to the MST if it does not form any cycle.
Step 6: Return the MST and exit
Kruskal’s Algorithm for Minimum Spanning Tree (MST)

Kruskal’s Algorithm

Step 1: Sort all the edges in non-decreasing order of their weight.

Step 2: Pick the smallest edge. Check if it forms a cycle with the spanning
tree formed so far. If the cycle is not formed, include this edge. Else, discard
it.

Step 3: Repeat step 2 until there are (V-1) edges in the spanning tree.
vertex with the fringe vertices.
Step 4: Find the minimum among these edges.
Step 5: Add the chosen edge to the MST if it does not form any cycle.
Step 6: Return the MST and exit
Difference between Prims and Kruskal Algorithm

S.No. Prim’s Algorithm Kruskal’s Algorithm

1 This algorithm begins to construct the shortest This algorithm begins to construct the
spanning tree from any vertex in the graph. shortest spanning tree from the vertex having
the lowest weight in the graph.
2 To obtain the minimum distance, it traverses It crosses one node only one time.
one node more than one time.
3 The time complexity of Prim’s algorithm is The time complexity of Kruskal’s algorithm is
O(V2). O(E log V).
4 In Prim’s algorithm, all the graph elements Kruskal’s algorithm may have disconnected
must be connected. graphs.
5 When it comes to dense graphs, the Prim’s When it comes to sparse graphs, Kruskal’s
algorithm runs faster. algorithm runs faster.
6 It prefers list data structure. It prefers the heap data structure.
Knapsack Problem - Greedy Method
1. Dijkstra Algorithm with Examples

2. Bellman Ford Algorithm with Examples

3. All Pairs shortest path (Warshal –Floyd)


Algorithm

Dijkstra Algorithm with Examples 70


Dijkstra Algorithm with Examples 71
72
Dijkstra Algorithm with Examples 73
74
Dijkstra Algorithm with Examples 75
76
Dijkstra Algorithm with Examples 77
78
Negative
weight
(demerit)

Dijkstra Algorithm with Examples 79


80
Dijkstra Algorithm with Examples 81
82
Dijkstra Algorithm with Examples 83
84
85
86
87
88
89
90
91
92
93
94
95
Demerit

96
97
98
99
100
All Pairs Shortest Path (Floyd-Warshall Algorithm)
Thank you

You might also like