cse 21 - lecture 3: graphs · cse 21: lecture3. graphs help us to visualize in a room there are 2n...

28
CSE 21 Lecture 3: Graphs CSE 21: Lecture3

Upload: others

Post on 26-Jul-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CSE 21 - Lecture 3: Graphs · CSE 21: Lecture3. Graphs help us to visualize In a room there are 2n people. Some of the people shake hand with each other in such a way that if persons

CSE 21Lecture 3: Graphs

CSE 21: Lecture3

Page 2: CSE 21 - Lecture 3: Graphs · CSE 21: Lecture3. Graphs help us to visualize In a room there are 2n people. Some of the people shake hand with each other in such a way that if persons

Course Website

Course details is available online:http://cseweb.ucsd.edu/classes/wi14/cse21-a/

Regular announcements will be made on the webpage

First Assignment is uploaded

Solutions to the assignment will be uploaded onMonday

Discussion forum set up in Piazza

CSE 21: Lecture3

Page 3: CSE 21 - Lecture 3: Graphs · CSE 21: Lecture3. Graphs help us to visualize In a room there are 2n people. Some of the people shake hand with each other in such a way that if persons

Quizes

There will be around 6 quizes (5% each)

Most of them will be in class.

Some online quizes will be given on WeBWork.

Total marks for online quizes will be 5%.

First quiz will be on Wednesday (15th January) inclass.

CSE 21: Lecture3

Page 4: CSE 21 - Lecture 3: Graphs · CSE 21: Lecture3. Graphs help us to visualize In a room there are 2n people. Some of the people shake hand with each other in such a way that if persons

Representation of data

Numbers - represented over any base. In particular onecan represent a number over binary.

Sets - binary vectors.

Functions - Truth table.

Relationship among elements -

CSE 21: Lecture3

Page 5: CSE 21 - Lecture 3: Graphs · CSE 21: Lecture3. Graphs help us to visualize In a room there are 2n people. Some of the people shake hand with each other in such a way that if persons

Representation of relation among elements

Vertices - set of elements.

V = {v1, . . . , vn}

Edges - set of pairs of vertices.

E = {e1, . . . , em}

ek = (vi, vj)

Given the set of vertices and edges we have a graph

G = (V,E)

CSE 21: Lecture3

Page 6: CSE 21 - Lecture 3: Graphs · CSE 21: Lecture3. Graphs help us to visualize In a room there are 2n people. Some of the people shake hand with each other in such a way that if persons

Representation of relation among elements

Vertices - set of elements.

V = {v1, . . . , vn}

Edges - set of pairs of vertices.

E = {e1, . . . , em}

ek = (vi, vj)

Given the set of vertices and edges we have a graph

G = (V,E)

CSE 21: Lecture3

Page 7: CSE 21 - Lecture 3: Graphs · CSE 21: Lecture3. Graphs help us to visualize In a room there are 2n people. Some of the people shake hand with each other in such a way that if persons

Representation of relation among elements

Vertices - set of elements.

V = {v1, . . . , vn}

Edges - set of pairs of vertices.

E = {e1, . . . , em}

ek = (vi, vj)

Given the set of vertices and edges we have a graph

G = (V,E)

CSE 21: Lecture3

Page 8: CSE 21 - Lecture 3: Graphs · CSE 21: Lecture3. Graphs help us to visualize In a room there are 2n people. Some of the people shake hand with each other in such a way that if persons

Basic Definitions

Let G = (V,E) be a graph.

If (u, v) ∈ E implies (v, u) ∈ E then it is called anundirected graph.

An weight can be assigned to each edge. In that case itis called an weighted graph.

CSE 21: Lecture3

Page 9: CSE 21 - Lecture 3: Graphs · CSE 21: Lecture3. Graphs help us to visualize In a room there are 2n people. Some of the people shake hand with each other in such a way that if persons

Basic Definitions

Let G = (V,E) be a graph.

If (u, v) ∈ E implies (v, u) ∈ E then it is called anundirected graph.

An weight can be assigned to each edge. In that case itis called an weighted graph.

CSE 21: Lecture3

Page 10: CSE 21 - Lecture 3: Graphs · CSE 21: Lecture3. Graphs help us to visualize In a room there are 2n people. Some of the people shake hand with each other in such a way that if persons

Basic Definitions

Let G = (V,E) be a graph.

If (u, v) ∈ E implies (v, u) ∈ E then it is called anundirected graph.

An weight can be assigned to each edge. In that case itis called an weighted graph.

CSE 21: Lecture3

Page 11: CSE 21 - Lecture 3: Graphs · CSE 21: Lecture3. Graphs help us to visualize In a room there are 2n people. Some of the people shake hand with each other in such a way that if persons

Basic Definitions

Let G = (V,E) be a graph.

If there is an edge from vertex u to v we say v is aneighbor of u

For an undirected graph the total number of u suchthat (u, v) ∈ E is called degree of v.

CSE 21: Lecture3

Page 12: CSE 21 - Lecture 3: Graphs · CSE 21: Lecture3. Graphs help us to visualize In a room there are 2n people. Some of the people shake hand with each other in such a way that if persons

Basic Definitions

Let G = (V,E) be a graph.

If there is an edge from vertex u to v we say v is aneighbor of u

For an undirected graph the total number of u suchthat (u, v) ∈ E is called degree of v.

CSE 21: Lecture3

Page 13: CSE 21 - Lecture 3: Graphs · CSE 21: Lecture3. Graphs help us to visualize In a room there are 2n people. Some of the people shake hand with each other in such a way that if persons

Basic Definitions

Let G = (V,E) be a graph.

If there is an edge from vertex u to v we say v is aneighbor of u

For an undirected graph the total number of u suchthat (u, v) ∈ E is called degree of v.

CSE 21: Lecture3

Page 14: CSE 21 - Lecture 3: Graphs · CSE 21: Lecture3. Graphs help us to visualize In a room there are 2n people. Some of the people shake hand with each other in such a way that if persons

Example of a graph

a

b c

d e

f g

Vertices

7

85

9

7

515

6

8

9

11

CSE 21: Lecture3

Page 15: CSE 21 - Lecture 3: Graphs · CSE 21: Lecture3. Graphs help us to visualize In a room there are 2n people. Some of the people shake hand with each other in such a way that if persons

Example of a graph

a

b c

d e

f g

Undirected Graph

7

85

9

7

515

6

8

9

11

CSE 21: Lecture3

Page 16: CSE 21 - Lecture 3: Graphs · CSE 21: Lecture3. Graphs help us to visualize In a room there are 2n people. Some of the people shake hand with each other in such a way that if persons

Example of a graph

a

b c

d e

f g

7

85

9

7

515

6

8

9

11

Weighted Undirected Graphs

CSE 21: Lecture3

Page 17: CSE 21 - Lecture 3: Graphs · CSE 21: Lecture3. Graphs help us to visualize In a room there are 2n people. Some of the people shake hand with each other in such a way that if persons

Example of a graph

a

b c

d e

f g

7

85

9

7

515

6

8

9

11

Weighted Directed Graphs

CSE 21: Lecture3

Page 18: CSE 21 - Lecture 3: Graphs · CSE 21: Lecture3. Graphs help us to visualize In a room there are 2n people. Some of the people shake hand with each other in such a way that if persons

How many edges can be there?

How many edges can be there is a simple undirected graphon n vertices?

CSE 21: Lecture3

Page 19: CSE 21 - Lecture 3: Graphs · CSE 21: Lecture3. Graphs help us to visualize In a room there are 2n people. Some of the people shake hand with each other in such a way that if persons

Advantages of a graph

Mathematical way of expressing relations amongobjects.

Very simple.

Very general.

CSE 21: Lecture3

Page 20: CSE 21 - Lecture 3: Graphs · CSE 21: Lecture3. Graphs help us to visualize In a room there are 2n people. Some of the people shake hand with each other in such a way that if persons

Example: Friendship graph

Each person is a vertex.

If two person are friends then there is an edge betweenthe respective vertices.

Used for understanding social networks, like facebook.

CSE 21: Lecture3

Page 21: CSE 21 - Lecture 3: Graphs · CSE 21: Lecture3. Graphs help us to visualize In a room there are 2n people. Some of the people shake hand with each other in such a way that if persons

Example: Friendship graph

CSE 21: Lecture3

Page 22: CSE 21 - Lecture 3: Graphs · CSE 21: Lecture3. Graphs help us to visualize In a room there are 2n people. Some of the people shake hand with each other in such a way that if persons

Example: Internet Graph

Every website is a vertex.

If a website has a link to another website then there isa directed edge from the first vertex to the second.

Used for web crawls by google.

CSE 21: Lecture3

Page 23: CSE 21 - Lecture 3: Graphs · CSE 21: Lecture3. Graphs help us to visualize In a room there are 2n people. Some of the people shake hand with each other in such a way that if persons

Example: Internet Graph

CSE 21: Lecture3

Page 24: CSE 21 - Lecture 3: Graphs · CSE 21: Lecture3. Graphs help us to visualize In a room there are 2n people. Some of the people shake hand with each other in such a way that if persons

Example: Road Network

Vertices are the cities

Edges are the roads

CSE 21: Lecture3

Page 25: CSE 21 - Lecture 3: Graphs · CSE 21: Lecture3. Graphs help us to visualize In a room there are 2n people. Some of the people shake hand with each other in such a way that if persons

Example: Road Network

CSE 21: Lecture3

Page 26: CSE 21 - Lecture 3: Graphs · CSE 21: Lecture3. Graphs help us to visualize In a room there are 2n people. Some of the people shake hand with each other in such a way that if persons

Other Examples

Many other problems in real life can be designed as aproblem in graph theory.

So studying the structure of graphs and designingalgorithms for graph problems is an important field.

CSE 21: Lecture3

Page 27: CSE 21 - Lecture 3: Graphs · CSE 21: Lecture3. Graphs help us to visualize In a room there are 2n people. Some of the people shake hand with each other in such a way that if persons

Graphs help us to visualize

In a room there are 2n people. Some of the people shakehand with each other in such a way that if persons A andB shake hand and persons B and C shake hand thenperson A and C does not shake hand.

What is the maximum number of handshakes possible inthis case?

Ans: n2

CSE 21: Lecture3

Page 28: CSE 21 - Lecture 3: Graphs · CSE 21: Lecture3. Graphs help us to visualize In a room there are 2n people. Some of the people shake hand with each other in such a way that if persons

Graphs help us to visualize

In a room there are 2n people. Some of the people shakehand with each other in such a way that if persons A andB shake hand and persons B and C shake hand thenperson A and C does not shake hand.

What is the maximum number of handshakes possible inthis case?

Ans: n2

CSE 21: Lecture3