Greedy_Algorithms_notes
Greedy_Algorithms_notes
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
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
25
Digraphs
Last time introduced digraphs as a way of
representing relations:
1 3
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).
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
Kruskal’s Algorithm
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
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
96
97
98
99
100
All Pairs Shortest Path (Floyd-Warshall Algorithm)
Thank you