33
Algorithms lecture notes 1 Introduction to Graphs Guy Kortsarz

Introduction to Graphscrab.rutgers.edu/~guyk/ex/graph1.pdf · Algorithms lecture notes 23 The longest path on a DAG is a polynomial question In a DAG all paths are simple. So to see

  • Upload
    others

  • View
    3

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Introduction to Graphscrab.rutgers.edu/~guyk/ex/graph1.pdf · Algorithms lecture notes 23 The longest path on a DAG is a polynomial question In a DAG all paths are simple. So to see

Algorithms lecture notes 1

Introduction to Graphs

Guy Kortsarz

Page 2: Introduction to Graphscrab.rutgers.edu/~guyk/ex/graph1.pdf · Algorithms lecture notes 23 The longest path on a DAG is a polynomial question In a DAG all paths are simple. So to see

Algorithms lecture notes 2

Graphs

An (undirected) graph G(V,E) has a collection

of vertices V = v1, v2 . . . vn of vertices with a

collection E = e1, e2, . . . , em of edges.Each edge connects two vertices.

Examples: Friendship graph. People are

vertices and there is an edge between them if

they are friends.

This assumes that friendship is symmetric.

But have you seen the film ”Cable Guy”?

Page 3: Introduction to Graphscrab.rutgers.edu/~guyk/ex/graph1.pdf · Algorithms lecture notes 23 The longest path on a DAG is a polynomial question In a DAG all paths are simple. So to see

Algorithms lecture notes 3

Driving among the nuts:

Philadelphia

I do not mean crazy people. I mean that they

have streets called Walnut and Chestnut.

Every intersection is a vertex.

An edge from a to b if there is a direct road

between a, b (you need to be able to go in both

directions).

Later we will have that: Directed graphs.

The web. Users are vertices and you connect

two vertices they are on their bookmarks.

Very likely to be directed in that case.

A LAN. Computers, that some are connected

and some are note. Passing messages between

computers can not be done directly but by

what I call a path.

Page 4: Introduction to Graphscrab.rutgers.edu/~guyk/ex/graph1.pdf · Algorithms lecture notes 23 The longest path on a DAG is a polynomial question In a DAG all paths are simple. So to see

Algorithms lecture notes 4

Graph terminology

In the above graph for e4 = (F,D) we say that

F and D touch e4, and that e4 touches F,D.

Or that this edge is incident on the vertex.

For edges we will say that (F,D) is an edge of

F but also an edge of D.

We also say that F and D are the two

endpoints of e4.

Two vertices are are independent if they share

no edge. Otherwise they are neighbors.

The degree of a vertex deg(v) is the number of

edges touching the vertex. For example

deg(F ) = 4 in the above graph.

A self loop adds 2 to the degree. And i parallel

edges add i to the degree of both endpoints.

A path is a walk over the edges for example

A− E −D −G−D − C.

A path is simple if it does not contain any

vertex more than once.

Page 5: Introduction to Graphscrab.rutgers.edu/~guyk/ex/graph1.pdf · Algorithms lecture notes 23 The longest path on a DAG is a polynomial question In a DAG all paths are simple. So to see

Algorithms lecture notes 5

The handshake lemma for

undirected graphs

The number of vertices of odd degree is always

even.

We shall explain why.

Let m be the number of edges then:∑

v∈V deg(v) = 2m.

This follows because each endpoint of an edge

contributes 2 to the sum

Page 6: Introduction to Graphscrab.rutgers.edu/~guyk/ex/graph1.pdf · Algorithms lecture notes 23 The longest path on a DAG is a polynomial question In a DAG all paths are simple. So to see

Algorithms lecture notes 6

Directed graphs

u 7→ v. You can not go from v to u but only

from u to v.

Can be represented by a matrix

degin(v) are the number of edges that enter v

degout(v) are the number of edges leaving v.

Clearly∑

v∈V degout(v) =∑

v∈V degin(v) = m.

When represented by linked lists we add to the

list of A[i] only vertices j so that i 7→ j exists.

Those who have edges into v are NOT his

neighbors

In fact the distance from v may be infinite.

Page 7: Introduction to Graphscrab.rutgers.edu/~guyk/ex/graph1.pdf · Algorithms lecture notes 23 The longest path on a DAG is a polynomial question In a DAG all paths are simple. So to see

Algorithms lecture notes 7

An example of a directed graph

Page 8: Introduction to Graphscrab.rutgers.edu/~guyk/ex/graph1.pdf · Algorithms lecture notes 23 The longest path on a DAG is a polynomial question In a DAG all paths are simple. So to see

Algorithms lecture notes 8

Representing directed graphs by a

matrix and linked lists

Say that the vertices are 1, 2, . . . , n.We build an n× n matrix and we put 1 in

A[i, j] if i 7→ j ∈ E and 0 otherwise.

Space n2.

Representing by linked lists

In A[i], A[i] points to the linked listt of

neighbors A[i] (the order could be any order

and depends on how the input was entered.).

Here the 0 of the Matrix are not represented

Page 9: Introduction to Graphscrab.rutgers.edu/~guyk/ex/graph1.pdf · Algorithms lecture notes 23 The longest path on a DAG is a polynomial question In a DAG all paths are simple. So to see

Algorithms lecture notes 9

The two possible representations of

a graph

1

2

3

4

5

8

6

1

2

3

4

5

6

7

8

2

3 1

NULL

5 1

3

7

NULL

6 4 57

1

2

3

4

5

6

7

8

0

0

0

0

0

0

0

0

1 2 3 4 5 6 7 81 0 0 0 0 0 0

1 0 0 0 0 0

0 0 0 0 0 0 0

1 0 0 1 0 0 0

0 0 1 0 0 0 0

0 0 0 0 0 1 0

0 0 0 0 0 0 0

0 0 0 1 1 1 1

1

7

Page 10: Introduction to Graphscrab.rutgers.edu/~guyk/ex/graph1.pdf · Algorithms lecture notes 23 The longest path on a DAG is a polynomial question In a DAG all paths are simple. So to see

Algorithms lecture notes 10

Computing the degrees of all

vertices

Say the graph is represented by a matrix.

1. For i = 1 to n do

(a) deg(i)← 0.

(b) For j = 1 to n do: if A[i, j] = 1 then

deg(i)← deg(i) + 1

2. Return D

The running time here is Θ(n2).

Page 11: Introduction to Graphscrab.rutgers.edu/~guyk/ex/graph1.pdf · Algorithms lecture notes 23 The longest path on a DAG is a polynomial question In a DAG all paths are simple. So to see

Algorithms lecture notes 11

Using linked list

We go to A[i] for every i, which is n.

In the directed case we spend degout(u) at U

and in total Θ(∑

u degout(u)) = Θ(m)

Total Θ(n+m) that may be much smaller than

n2.

For undirected graph the sum is 2m which is

insignificant.

Page 12: Introduction to Graphscrab.rutgers.edu/~guyk/ex/graph1.pdf · Algorithms lecture notes 23 The longest path on a DAG is a polynomial question In a DAG all paths are simple. So to see

Algorithms lecture notes 12

Relation between the number of

vertices n and the number of edges

m

If the graph is connected (one piece or finite

distance between any two vertices) we shall see

that m ≥ n− 1.

The maximum m can be is(

n2

)

< n2. Thus

m = O(n2).

For example (assuming no self loops or parallel

edges),√m = O(n).

The distance between a and b is the number

of edges in the shortest path between a, b

Do not say shortest distance. Distance is

already shortest.

Page 13: Introduction to Graphscrab.rutgers.edu/~guyk/ex/graph1.pdf · Algorithms lecture notes 23 The longest path on a DAG is a polynomial question In a DAG all paths are simple. So to see

Algorithms lecture notes 13

Distances

A B

C D E

X Y

THE DISTANCE BETWEEN A AND B IS 3

THE DISTANCE BETWEEN X AND A IS INFINITE.

THE DISTANCE BETWEEN X AND E IS 2

Page 14: Introduction to Graphscrab.rutgers.edu/~guyk/ex/graph1.pdf · Algorithms lecture notes 23 The longest path on a DAG is a polynomial question In a DAG all paths are simple. So to see

Algorithms lecture notes 14

A remark

Say that we add a collection of odd numbers.

Note that the sum is even if and only if

the amount of numbers is even.

In fact adding an odd number changes the

even/non-even status.

While adding even numbers leaves the status as

it was

Page 15: Introduction to Graphscrab.rutgers.edu/~guyk/ex/graph1.pdf · Algorithms lecture notes 23 The longest path on a DAG is a polynomial question In a DAG all paths are simple. So to see

Algorithms lecture notes 15

The number of odd degree vertices

We prove that the number of odd degree

vertices is always even.

A vertex can have either odd or even degree.

The two set VO of odd degree vertices and VE

of even degree vertices are disjoint.

So we can say that∑

v∈V deg(v) =∑

v∈VOdeg(v) +

v∈VEdeg(v) = 2m

The last equality we have shown before.

Page 16: Introduction to Graphscrab.rutgers.edu/~guyk/ex/graph1.pdf · Algorithms lecture notes 23 The longest path on a DAG is a polynomial question In a DAG all paths are simple. So to see

Algorithms lecture notes 16

Continued

Thus∑

v∈Vo= 2m−∑

v∈VEdeg(v).

Note that∑

v∈VEdeg(v) is an even number.

This implies that∑

v∈Vo= 2m−∑

v∈VEdeg(v)

is even.

By the comment we made before, this implies

that |VO| is even

Page 17: Introduction to Graphscrab.rutgers.edu/~guyk/ex/graph1.pdf · Algorithms lecture notes 23 The longest path on a DAG is a polynomial question In a DAG all paths are simple. So to see

Algorithms lecture notes 17

Directed acyclic graphs

Directed graph not containing directed cycles.

All paths are simple.

Extending paths in a DAG (Directed Acyclic

graph) shows two interesting things.

Say we start at u0 7→ u1 as a directed edge.

If there is an edge z1 7→ u0 we add it getting a

path z1 7→ u0 7→ u1

And z2 7→ z1 7→ u0 7→ u1.

Note that every z is a new vertex for otherwise

we got a cycle. This must finish. Hence we

have shown:

Remark: Every DAG has at least one vertex

of indegree 0.

The same argument shows that every DAG has

a vertex with out degree 0

Page 18: Introduction to Graphscrab.rutgers.edu/~guyk/ex/graph1.pdf · Algorithms lecture notes 23 The longest path on a DAG is a polynomial question In a DAG all paths are simple. So to see

Algorithms lecture notes 18

An example of a directed graph

with no directed cycles

A

B

C

D

E

F

G

Page 19: Introduction to Graphscrab.rutgers.edu/~guyk/ex/graph1.pdf · Algorithms lecture notes 23 The longest path on a DAG is a polynomial question In a DAG all paths are simple. So to see

Algorithms lecture notes 19

Topological sorting

Claim: in a DAG you have vertices of indegree

0 and outdegree 0.

Proof: extend a path until you cant, namely x1

the first vertex has no entering edges to new

vertices and the vertex and xk at the end does

not have out edges to new vertices.

Let P be path. There could not be an edge

from xp into x1, 1 < p as otherwise we get a

cycle. There could not be an edge from xk into

the path as this gives a cycle.

This means that x1 has indegree 0 and xk has

outdegree 0.

A topological sorting is arranging thee vertices

us u1, u2, . . . , un on a line so that every edge

that start at some vertex ui and end on uj has

i < j (all edges go right).

In general directed graph a cycle of size 3

implies that this is not possible.

Page 20: Introduction to Graphscrab.rutgers.edu/~guyk/ex/graph1.pdf · Algorithms lecture notes 23 The longest path on a DAG is a polynomial question In a DAG all paths are simple. So to see

Algorithms lecture notes 20

An algorithm to find a topological

ordering on a DAG

1. W ← V , G′ ← G.

2. While W 6= ∅ do:(a) recompute the in degrees in G′

(b) Let w be a vertex of indegree 0 in G′

(c) Output w as next in the order

(d) W ←W − w and G′ ← G− w

3. Return the ordering

Page 21: Introduction to Graphscrab.rutgers.edu/~guyk/ex/graph1.pdf · Algorithms lecture notes 23 The longest path on a DAG is a polynomial question In a DAG all paths are simple. So to see

Algorithms lecture notes 21

A DAG and one of the topological

orderings

A

B

C

D

E

F

G

A G F C B D E

Page 22: Introduction to Graphscrab.rutgers.edu/~guyk/ex/graph1.pdf · Algorithms lecture notes 23 The longest path on a DAG is a polynomial question In a DAG all paths are simple. So to see

Algorithms lecture notes 22

Why does it work

When adding the i consider a vertex j < i.

By definition after we remove 1, 2, . . . , j − 1

deg(j) = 0.

Which by definition means that vertices in

W = V − 1, 2, . . . , j − 1 do not have edges

into j.

This is a partial order. Edges go only to the

right. But some vertices can not be compared.

If we have a cycle of size 3 no matter the

ordering, one edge will go backward and so

there is no topological ordering

Page 23: Introduction to Graphscrab.rutgers.edu/~guyk/ex/graph1.pdf · Algorithms lecture notes 23 The longest path on a DAG is a polynomial question In a DAG all paths are simple. So to see

Algorithms lecture notes 23

The longest path on a DAG is a

polynomial question

In a DAG all paths are simple.

So to see if there is a paths of size k or more

can be done in polynomial time.

Try all vertices as start vertices of the path.

For v you go to the neighbors of v, and give

them distance 1, and go the the neighbors of

neighbors that did not get a distance yet and

give them distance 2. And so on. If the graph

is not exhausted before we get a path of length

k, it exists. This is because every new neighbor

did not appear before.

In general directed graphs this would have

produced cycles. Hence non simple paths hence

fails.

A famous NPC problem the longest path can

be solved in DAGS. In general directed graph it

does not work since the paths we find are not

simple.

Page 24: Introduction to Graphscrab.rutgers.edu/~guyk/ex/graph1.pdf · Algorithms lecture notes 23 The longest path on a DAG is a polynomial question In a DAG all paths are simple. So to see

Algorithms lecture notes 24

Some problems in graphs

The Independent set problem:

Input: An undirected graph and a number k

Question: Does this graph admits an

independent set of size at least k (in an

independent set no two vertices share an edge)

The Clique problem:

Input: An undirected graph and a number k

Question: Does this graph admit a subset X

of size at least k so that every two vertices in

X share an edge?

The Vertex cover problem:

Input: An undirected graph and a number k

Question: Does this graph admit a subset X

of size at most k so that X has at least one

endpoint of every edge?

Page 25: Introduction to Graphscrab.rutgers.edu/~guyk/ex/graph1.pdf · Algorithms lecture notes 23 The longest path on a DAG is a polynomial question In a DAG all paths are simple. So to see

Algorithms lecture notes 25

Cliques, independent sets and

vertex covers in the previous graph

A

B

C

D

E

F

G

e1

e 2

e 3

e 4

e

e 6

e7

e85

e9

e10

e11

Figure 1: A graph

Largest clique here size 3: say E,A, F.Largest independent set size 3: E,B,Csmallest vertex cover A,F,D,G (if V ′ is an

independent set V − V ′ is a vertex cover and

vise-versa).

Page 26: Introduction to Graphscrab.rutgers.edu/~guyk/ex/graph1.pdf · Algorithms lecture notes 23 The longest path on a DAG is a polynomial question In a DAG all paths are simple. So to see

Algorithms lecture notes 26

Two more examples

The Hamiltonian path (or Hamiltonian cycle)

problem

Input: An undirected graph.

Question: Is there a simple path in the graph

that contains all the vertices and starts with

two different vertices?

In the Hamiltonian cycle problem the path has

to start and end with the same vertex (this is

called a cycle).

The Traveling Sales Person problem:

Input : A Complete graph (all the edges exist)

and a cost c(e) associated with every edge e,

and a number k

Question: Does there exists an Hamiltonian

path so that the sum of cost of its edges is at

most k?

The most “famous” problem in computer

science.

Page 27: Introduction to Graphscrab.rutgers.edu/~guyk/ex/graph1.pdf · Algorithms lecture notes 23 The longest path on a DAG is a polynomial question In a DAG all paths are simple. So to see

Algorithms lecture notes 27

A tricky question: what is the size

of the input?

Input: A number n

Question: Is the number a prime?

Obviously you can try all (odd) numbers and

divide and see. But this takes Ω(n).

The size of the input: how many bits does it

take to store the input.

A number n requires only ⌈log2 n⌉ bits to store

(under binary representation) hence the size of

the input is O(log n) not n. A polynomial

algorithm for the above must run in time say

(log2 n)8.

Page 28: Introduction to Graphscrab.rutgers.edu/~guyk/ex/graph1.pdf · Algorithms lecture notes 23 The longest path on a DAG is a polynomial question In a DAG all paths are simple. So to see

Algorithms lecture notes 28

Two famous examples, one with a

happy ending

The Graph isomorphism problem.

Input: Two graphs G(V,E) and H(U,E′) with

|V | = |U | and |E| = |E′|Question: Is there a 1 to 1 and onto function

f : V 7→ H so that (u, v) ∈ E iff

(f(u), f(v)) ∈ E′?

This means that G,H are the same graph with

different names.

Obviously in NP . Not known to be in P, not

known to be NPC.

Now, a problem that endured 300 years:

Input: A number n

Question: Is n a prime number?

Solved a few years ago and shown to be in P.

(Dates back to Gauss). The initial running

time was O((log n)6)!

Page 29: Introduction to Graphscrab.rutgers.edu/~guyk/ex/graph1.pdf · Algorithms lecture notes 23 The longest path on a DAG is a polynomial question In a DAG all paths are simple. So to see

Algorithms lecture notes 29

The P versus NPC dilemma

NPC are problems for which we can check if a

solution is valid fast. But there are too many

possible solutions so the simple algorithm is

exponential.

The solution for HP is an ordering of the

vertices in a line. It takes O(n) times to check

that it is really a solution.

Page 30: Introduction to Graphscrab.rutgers.edu/~guyk/ex/graph1.pdf · Algorithms lecture notes 23 The longest path on a DAG is a polynomial question In a DAG all paths are simple. So to see

Algorithms lecture notes 30

Checking the validity of a solution

for clique

The answer is some set W of size at least k.

There are at most(

n2

)

< n2 pairs, that we need

to check that are connected.

For the Maximum independent set it is the

same time.

For the TSP the answer is an ordering on a line

and the time to check is O(n)

For vertex Cover we are given a set. We can

check in time at most O(n2) tat everybody

outside the VC has a neighbor in the VC

Page 31: Introduction to Graphscrab.rutgers.edu/~guyk/ex/graph1.pdf · Algorithms lecture notes 23 The longest path on a DAG is a polynomial question In a DAG all paths are simple. So to see

Algorithms lecture notes 31

The number of solutions

Is there a clique of size k?(

n

k

)

.

If k is k = n/2 the above is roughly 2n. So we

can not check in poly time all subsets of a set.

The number of orderings on a line is Θ(n!)

And n! > 2n

Page 32: Introduction to Graphscrab.rutgers.edu/~guyk/ex/graph1.pdf · Algorithms lecture notes 23 The longest path on a DAG is a polynomial question In a DAG all paths are simple. So to see

Algorithms lecture notes 32

Problems that can be solved in

polynomial time

Euler Path: is there a path that goes over all

edges exactly once? Answer: if the number of

odd degree vertices is 0 or 2 the answer is yes.

If their number is at least 4 the answer is no.

As opposed to ”is there a path that goes over

all the vertices once”, or HP. This problem is

NPC.

Find the shortest path from s to t (even with

weights) is in P .

As oppose to find the maximum simple path

between s and t which is NPC.

Page 33: Introduction to Graphscrab.rutgers.edu/~guyk/ex/graph1.pdf · Algorithms lecture notes 23 The longest path on a DAG is a polynomial question In a DAG all paths are simple. So to see

Algorithms lecture notes 33

Other well known problems in P

Minimum Spanning tree (Maximum Spanning

tree is also in P )

The flow problem (see later).

Recall that a matching is a collection of edges

no two of which have the same end-point.

Maximum size matching can be solved in

general graph in polynomial time (albeit

complex).

Given a number k and v is there k vertex

disjoint paths from v to every vertex in V − v?

is a polynomial problem.

Minimum cut: Given a graph G find two non

empty sets S1, S2, so that S1 ∪ S2 = V ,

S1 ∩ S2 = ∅, and minimize the number of edges

with one endpoint in S1 and the other in S2. A

polynomial problems. As opposed to

Max− Cut which is NPC.