Lecture 1. Introduction Haodi Feng Sch. of Comp. Sci. and Tech. Shandong University Email:...

38
Lecture 1. Introduction Haodi Feng Sch. of Comp. Sci. and Tech. Shandong University Email: fenghaodi @sdu.edu.cn Office: Rm430, Computing Center

Transcript of Lecture 1. Introduction Haodi Feng Sch. of Comp. Sci. and Tech. Shandong University Email:...

Page 1: Lecture 1. Introduction Haodi Feng Sch. of Comp. Sci. and Tech. Shandong University Email: fenghaodi@sdu.edu.cn@sdu.edu.cn Office: Rm430, Computing Center.

Lecture 1. Introduction

Haodi FengSch. of Comp. Sci. and Tech.

Shandong University

Email: [email protected] Office: Rm430, Computing Center

Page 2: Lecture 1. Introduction Haodi Feng Sch. of Comp. Sci. and Tech. Shandong University Email: fenghaodi@sdu.edu.cn@sdu.edu.cn Office: Rm430, Computing Center.

2004SDU 2

Text Book & Reference Books

Text Book: Introduction to Algorithms (Second Edition)

– Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest and Clifford Stein

– Higher Education Press, The MIT Press, 68RMB

Reference Books Algorithm Design Techniques and Analysis

– M.H. Alsuwaiyel (Saudi Arabia), 电子工业出版社, 40元。 《图算法》:马绍汉编著,山东大学出版社。 《算法设计与分析》:王晓东编著,清华大学出版社, 30元。 《计算机算法导引—设计与分析》:卢开澄编著,清华大学出版社, 21元。

Page 3: Lecture 1. Introduction Haodi Feng Sch. of Comp. Sci. and Tech. Shandong University Email: fenghaodi@sdu.edu.cn@sdu.edu.cn Office: Rm430, Computing Center.

2004SDU 3

Grading Scheme

Total 100 Assignment 10% Final Examination: 90%

Page 4: Lecture 1. Introduction Haodi Feng Sch. of Comp. Sci. and Tech. Shandong University Email: fenghaodi@sdu.edu.cn@sdu.edu.cn Office: Rm430, Computing Center.

2004SDU 4

Contents

Part I Explore techniques on a graph

– Breadth-first search algorithm (BFS)

– Depth-first search algorithm (DFS)

Applications of DFS – Classifying edges (for directed or undirected)

– Topological sort (for directed acyclic)

– Strongly connected components decomposing (for directed)

– Bi-connected components decomposing (for undirected)

Page 5: Lecture 1. Introduction Haodi Feng Sch. of Comp. Sci. and Tech. Shandong University Email: fenghaodi@sdu.edu.cn@sdu.edu.cn Office: Rm430, Computing Center.

2004SDU 5

Contents (continued)

Part II Minimum spanning tree problem (for undirected)

– Kruskal’s algorithm– Prim’s algorithm

Bottleneck spanning tree problem

Part III Single-source shortest paths problem (for directed)

– Bellman-Ford algorithm– Dijkstra’s algorithm

All-pairs shortest paths problem– Matrix-multiplication based algorithm– Floyd’s algorithm– Johnson’s algorithm

Page 6: Lecture 1. Introduction Haodi Feng Sch. of Comp. Sci. and Tech. Shandong University Email: fenghaodi@sdu.edu.cn@sdu.edu.cn Office: Rm430, Computing Center.

2004SDU 6

Contents (continued)

Part IV Maximum flow problem (for network)

– Ford-Fulkerson algorithm

– Dinic’s algorithm

Applications of Max-flow problem– Maximum matching algorithm for Bipartite graphs

Part V Maximum Matching

Page 7: Lecture 1. Introduction Haodi Feng Sch. of Comp. Sci. and Tech. Shandong University Email: fenghaodi@sdu.edu.cn@sdu.edu.cn Office: Rm430, Computing Center.

2004SDU 7

Prerequisite

Data Structure

C, Java or other programming languages

A little mathematics

Page 8: Lecture 1. Introduction Haodi Feng Sch. of Comp. Sci. and Tech. Shandong University Email: fenghaodi@sdu.edu.cn@sdu.edu.cn Office: Rm430, Computing Center.

2004SDU 8

Graph Preliminary Knowledge

In this class, try to answer three questions: What is a graph? How to represent a graph? Basic properties of a graph

Page 9: Lecture 1. Introduction Haodi Feng Sch. of Comp. Sci. and Tech. Shandong University Email: fenghaodi@sdu.edu.cn@sdu.edu.cn Office: Rm430, Computing Center.

2004SDU 9

Graph Basics (Graph)

A graph G is a pair (V, E), where V is a finite set and E is a set of pairs on V Denote as G = (V, E) Element of V: Vertex; Element of E: Edge;

Undirected graph (u. w. l. g., graph): Each pair in E is unordered, i.e., for each pair (u, v) E, (u, v) = (v, u). Directed graph: Each pair in E is ordered, i.e., for each pair (u, v) E, (u, v) (v, u), and either (v, u) E or not.

G1 = ((1, 2, 3), ((1, 2), (2, 3), (3,1)))

G2 = ((1, 2, 3), ((1, 2), (2, 3), (1,4)))?

Page 10: Lecture 1. Introduction Haodi Feng Sch. of Comp. Sci. and Tech. Shandong University Email: fenghaodi@sdu.edu.cn@sdu.edu.cn Office: Rm430, Computing Center.

2004SDU 10

Schematic Representation

v1 v2

v3v4

v5

v6

e1

e2 e3 e7

v1 v2

v3v4

v5

v6

e1

e2 e3

e4

e5

e6

e7

Can you give the representation in format G = (V, E) for above?

e4

Page 11: Lecture 1. Introduction Haodi Feng Sch. of Comp. Sci. and Tech. Shandong University Email: fenghaodi@sdu.edu.cn@sdu.edu.cn Office: Rm430, Computing Center.

2004SDU 11

Graph Basics (Adjacency)

Adjacency: In a graph G = (V, E), if (u, v) E, say that vertex v is adjacent to vertex u. If G is an undirected graph, vertex u is adjacent to

vertex v, too. The adjacency relation is symmetric. We also say (u, v) is incident with u and v, vice versa. If G is a directed graph, if (u, v) E, vertex v is

adjacent to vertex u, vertex u is not necessarily adjacent to vertex v. (u, v) is incident from or leaves vertex u and is incident to or enters vertex v.

Two edges are called to be adjacent if they share a common vertex.

Page 12: Lecture 1. Introduction Haodi Feng Sch. of Comp. Sci. and Tech. Shandong University Email: fenghaodi@sdu.edu.cn@sdu.edu.cn Office: Rm430, Computing Center.

2004SDU 12

Graph Basics (Degree)

Degree of a vertex v: For undirected graph:

– Number of edges incident with v.

– Denote as dG(v) or simply, d(v).

For directed graph: – In-degree: Number of edges entering (or incident to) v.

– Out-degree: Number of edges leaving (or incident from) v.

– Denote as idG(v), odG(v) or simply, id(v), od(v) respectively.

Page 13: Lecture 1. Introduction Haodi Feng Sch. of Comp. Sci. and Tech. Shandong University Email: fenghaodi@sdu.edu.cn@sdu.edu.cn Office: Rm430, Computing Center.

2004SDU 13

Graph Properties

Theorem 1.1 (Handshaking Lemma) Given an undirected graph G = (V, E), the sum of degrees of all

vertices is that , where | E | is the number of edges in G.

Proof: Can you give? How about a directed graph? (sum of in-degrees equals to sum of

out-degrees, i.e., the number of edges)

Corollary 1.2 Given a graph G = (V, E), the number of vertices with odd

degrees is even. Proof: Can you give?

( ) 2 | |v V

d v E

Page 14: Lecture 1. Introduction Haodi Feng Sch. of Comp. Sci. and Tech. Shandong University Email: fenghaodi@sdu.edu.cn@sdu.edu.cn Office: Rm430, Computing Center.

2004SDU 14

Graph Basics (Path)

A path of length k from a vertex u to a vertex u’ in a graph G = (V, E) is a sequence <v0, v1, v2, …, vk> of vertices such that u = v0, u’ = vk, and (vi-1, vi) E for i = 1, 2, …, k. Length of a path: Number of edges in the path u’ is reachable from u via p if there is a path p from u to u’, write

as u u’, sometimes. For undirected graph, we also call u and u’ are connected.

Simple path: All vertices in the path are distinct. A sub-path of path p = <v0, v1, v2, …, vk> is a contiguous

subsequence of its vertices.– For any 0 i j k, the subsequence of vertices < vi, vi+1, …, vj >

p

Page 15: Lecture 1. Introduction Haodi Feng Sch. of Comp. Sci. and Tech. Shandong University Email: fenghaodi@sdu.edu.cn@sdu.edu.cn Office: Rm430, Computing Center.

2004SDU 15

Graph Basics (Cycle)

A cycle is a path p = <v0, v1, v2, …, vk> with v0 = vk. For undirected graph, a simple cycle contains at least

3 edges, that is, k 3 and v1, v2, …, vk are distinct.

For directed graph, a simple cycle contains at least 1 edge, and v1, v2, …, vk are distinct.

Acyclic graph: A graph with no cycles. DAG: Directed acyclic graph.

Page 16: Lecture 1. Introduction Haodi Feng Sch. of Comp. Sci. and Tech. Shandong University Email: fenghaodi@sdu.edu.cn@sdu.edu.cn Office: Rm430, Computing Center.

2004SDU 16

Graph Basics (Sub-graph)

A graph G’ = (V’, E’) is a sub-graph of G = (V, E) if V’ V and E’ E. A graph G’’ = (V’’, E’’) is a vertex induced sub-graph

of G = (V, E) if V’’ V and E’’ = {(u, v) E: u, v V’’}.

G G’ G’’

Page 17: Lecture 1. Introduction Haodi Feng Sch. of Comp. Sci. and Tech. Shandong University Email: fenghaodi@sdu.edu.cn@sdu.edu.cn Office: Rm430, Computing Center.

2004SDU 17

Graph Basics (Connected)

An undirected graph is connected if every pair of vertices is connected by a path (reachable from each other). Connected component of a graph is a sub-graph such that: 1) It is

connected; 2) Any other sub-graph that contains it as a true sub-graph is not connected.

Any connected, undirected graph G = (V, E) satisfies that | E | | V | -1. (Hints: Adding each edge decreases # of connected

components by at most one)

A directed graph is strongly connected if every two vertices are reachable from each other. Strongly connected component of a directed graph is a sub-graph

such that: 1) It is strongly connected; 2) Any other sub-graph that contains it as a true sub-graph is not strongly connected.

Page 18: Lecture 1. Introduction Haodi Feng Sch. of Comp. Sci. and Tech. Shandong University Email: fenghaodi@sdu.edu.cn@sdu.edu.cn Office: Rm430, Computing Center.

2004SDU 18

Connectedness (exa.)

G

F Other SCCs?

Page 19: Lecture 1. Introduction Haodi Feng Sch. of Comp. Sci. and Tech. Shandong University Email: fenghaodi@sdu.edu.cn@sdu.edu.cn Office: Rm430, Computing Center.

2004SDU 19

Special graphs

A complete graph is an undirected graph in which every pair of vertices are adjacent.

A bipartite graph is a graph G = (V, E) in which V can be partitioned into two disjoint sets V1 and V2 such that (u, v) E implies either uV1 and vV1 or uV2 and vV2.

A forest is an acyclic, undirected graph.

A tree is a connected, acyclic, undirected graph. (|E|=|V|-1) (proof? Induction on # of vertices)

Page 20: Lecture 1. Introduction Haodi Feng Sch. of Comp. Sci. and Tech. Shandong University Email: fenghaodi@sdu.edu.cn@sdu.edu.cn Office: Rm430, Computing Center.

2004SDU 20

Sparse vs. Dense

Sparse graph: | E | is much less than | V |2

Dense graph: | E | is close to | V |2

“Spare” and “dense” are relative concepts.

Page 21: Lecture 1. Introduction Haodi Feng Sch. of Comp. Sci. and Tech. Shandong University Email: fenghaodi@sdu.edu.cn@sdu.edu.cn Office: Rm430, Computing Center.

2004SDU 21

Asymptotic Notation (O)

O-notation: Asymptotic upper bound O(g(n)) = {f(n): there exist positive constants c and n0

such that 0 f(n) cg(n) for all n n0}.– f(n) = 2n3+3n-5 (or =) O(n3)

– f(n) = 2n3+3n-5 (or =)O(n4)

Page 22: Lecture 1. Introduction Haodi Feng Sch. of Comp. Sci. and Tech. Shandong University Email: fenghaodi@sdu.edu.cn@sdu.edu.cn Office: Rm430, Computing Center.

2004SDU 22

Asymptotic Notation (O)

n

f(n)

cg(n)

n0

f(n) = O(g(n))

Page 23: Lecture 1. Introduction Haodi Feng Sch. of Comp. Sci. and Tech. Shandong University Email: fenghaodi@sdu.edu.cn@sdu.edu.cn Office: Rm430, Computing Center.

2004SDU 23

Asymptotic Notation ()

-notation: Asymptotic lower bound (g(n)) = {f(n): there exist positive constants c and n0

such that 0 cg(n) f(n) for all n n0}.– f(n) = 2n3+3n-5 (or =) (n3)

– f(n) = 2n3+3n-5 (or =) (n2)

Page 24: Lecture 1. Introduction Haodi Feng Sch. of Comp. Sci. and Tech. Shandong University Email: fenghaodi@sdu.edu.cn@sdu.edu.cn Office: Rm430, Computing Center.

2004SDU 24

Asymptotic Notation ()

n

cg(n)

f(n)

n0

f(n) = (g(n))

Page 25: Lecture 1. Introduction Haodi Feng Sch. of Comp. Sci. and Tech. Shandong University Email: fenghaodi@sdu.edu.cn@sdu.edu.cn Office: Rm430, Computing Center.

2004SDU 25

Asymptotic Notation ()

-notation: Exact order (g(n)) = {f(n): there exist positive constants c1, c2,

and n0 such that 0 c1g(n) f(n) c2g(n) for all n n0}.– f(n) = 2n3+3n-5 (or =) (n3)

Page 26: Lecture 1. Introduction Haodi Feng Sch. of Comp. Sci. and Tech. Shandong University Email: fenghaodi@sdu.edu.cn@sdu.edu.cn Office: Rm430, Computing Center.

2004SDU 26

Asymptotic Notation ()

n

f(n)

c2g(n)

n0

f(n) = (g(n))

c1g(n)

Page 27: Lecture 1. Introduction Haodi Feng Sch. of Comp. Sci. and Tech. Shandong University Email: fenghaodi@sdu.edu.cn@sdu.edu.cn Office: Rm430, Computing Center.

2004SDU 27

Representations of graphs

Two standard ways: A collection of adjacency lists An adjacency matrix

Page 28: Lecture 1. Introduction Haodi Feng Sch. of Comp. Sci. and Tech. Shandong University Email: fenghaodi@sdu.edu.cn@sdu.edu.cn Office: Rm430, Computing Center.

2004SDU 28

Adjacency-list representation

Consist of an array Adj of | V | lists, one for each vertex in V.Adjacency list Adj[u] contains all the vertices v such that there is an edge (u, v) E, i.e., all vertices that are adjacent to u.Vertices in Adj[u] can be stored in arbitrary order.Sum of the lengths of all the adjacency lists is | E | if G is a directed graph 2| E | if G is an undirected graph

Memory needed to store an adjacency-list representation of a graph is (| V | + | E |)Weighted graph can be represented by adjacency lists.

Page 29: Lecture 1. Introduction Haodi Feng Sch. of Comp. Sci. and Tech. Shandong University Email: fenghaodi@sdu.edu.cn@sdu.edu.cn Office: Rm430, Computing Center.

2004SDU 29

Representations of graphs (Examples)

Page 30: Lecture 1. Introduction Haodi Feng Sch. of Comp. Sci. and Tech. Shandong University Email: fenghaodi@sdu.edu.cn@sdu.edu.cn Office: Rm430, Computing Center.

2004SDU 30

Representations of graphs (Examples)

Page 31: Lecture 1. Introduction Haodi Feng Sch. of Comp. Sci. and Tech. Shandong University Email: fenghaodi@sdu.edu.cn@sdu.edu.cn Office: Rm430, Computing Center.

2004SDU 31

Adjacency-list representation (advantage vs. disadvantage)

Advantage: When the graph is sparse, uses only O(| V | + | E |)

memory.

Disadvantage: No quicker way to determine if a given edge (u, v) is

present in the graph than to search for v in the adjacency list Adj[u]. (How much time needed?)

Page 32: Lecture 1. Introduction Haodi Feng Sch. of Comp. Sci. and Tech. Shandong University Email: fenghaodi@sdu.edu.cn@sdu.edu.cn Office: Rm430, Computing Center.

2004SDU 32

Adjacency-matrix representation

Consists of a | V | | V | matrix A = (aij) such that

or

A is symmetric along the main diagonal for undirected graphs, that is, AT = A.

For directed graph, generally, AT A. Can you imagine what directed graph satisfies AT = A?

Adjacency-matrix can be used to represent weighted graphs.

otherwise 0

,),( if 1 Ejiaij

otherwise

,),( if 1 Ejiaij

Page 33: Lecture 1. Introduction Haodi Feng Sch. of Comp. Sci. and Tech. Shandong University Email: fenghaodi@sdu.edu.cn@sdu.edu.cn Office: Rm430, Computing Center.

2004SDU 33

Adjacency-matrix representation (Advantage vs. disadvantage)

Advantage: Easily or quickly to determine if an edge is in the

graph or not by just looking at the proper position of the matrix.

Disadvantage: Uses more memory to store a graph. O(| V |2)

Page 34: Lecture 1. Introduction Haodi Feng Sch. of Comp. Sci. and Tech. Shandong University Email: fenghaodi@sdu.edu.cn@sdu.edu.cn Office: Rm430, Computing Center.

2004SDU 34

Properties of adjacency-matrix representation

Lemma 1.3 Suppose that A is the adjacency-matrix representation of an

undirected graph G, then the element Ak[i, j] of Ak = AA… A is the number of paths of length k that connect vertices i and j.

Proof by induction on k.– Basic step: k = 1, A1 = A, A[i, j] is the number of paths of length 1

that connect vertices i and j.– Inductive step: Suppose it is true for k >1 and the numbers smaller

than k, that is, Ak[i, j] is the number of paths of length k that connect vertices i and j. From Ak+1=A Ak, we have Ak+1[i, j] = lV A[i, l] Ak[l, j]. For A[i, l] is the number of paths of length 1 that connect vertices i and l, Ak[l, k] is the number of paths of length k that connect vertices l and j. So, A[i, l] Ak[l, j] is the number of paths of length k+1 that connect vertices i and j via l. Consider all vertices l, 1 l n, we get our conclusion.

Page 35: Lecture 1. Introduction Haodi Feng Sch. of Comp. Sci. and Tech. Shandong University Email: fenghaodi@sdu.edu.cn@sdu.edu.cn Office: Rm430, Computing Center.

2004SDU 35

Properties of adjacency-matrix representation

2

1 1

[ , ] [ , ] [ , ] ( )n n

Gj j

A i i A i j A j i d i

Corollary 1.4

Proof: For symmetry of adjacency-matrix,

A[i, l]=A[l, i] = 0 or 1, so

2

1 1 1

[ , ] [ , ] [ , ] [ , ] [ , ] ( )n n n

Gl j j

A i i A i l A l i A i j A j i d i

1 2

43

A2[1, 1]: 2: (1, 2, 1), (1, 3, 1)

A2[2, 2]: 3: (2, 1, 2), (2, 3, 2), (2, 4, 2)

Page 36: Lecture 1. Introduction Haodi Feng Sch. of Comp. Sci. and Tech. Shandong University Email: fenghaodi@sdu.edu.cn@sdu.edu.cn Office: Rm430, Computing Center.

2004SDU 36

Euler Graph

History: Leonhard Euler (1707 -1783)

In Konigsberg, German, a river ran through the city such that in its center was an island, and after passing the island, the river broke into two parts. Seven bridges were built so that the people of the city could get from one part to another.

Page 37: Lecture 1. Introduction Haodi Feng Sch. of Comp. Sci. and Tech. Shandong University Email: fenghaodi@sdu.edu.cn@sdu.edu.cn Office: Rm430, Computing Center.

2004SDU 37

Knigsberg Bridges Problem

People wondered whether or not one could walk around the city in a way that would involve crossing each bridge exactly once. Now, have a try!

How about if a bridge is removed?

Page 38: Lecture 1. Introduction Haodi Feng Sch. of Comp. Sci. and Tech. Shandong University Email: fenghaodi@sdu.edu.cn@sdu.edu.cn Office: Rm430, Computing Center.

2004SDU 38

Why?