graphs - yeasirrayhanprince.github.io · 0 undirected graph: vertices and edges edges = {02, 05,...

44
Graphs

Upload: others

Post on 16-Jul-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Graphs - yeasirrayhanprince.github.io · 0 Undirected Graph: Vertices and edges Edges = {02, 05, 04, 15, 14, 24, 23, 45 } No convention Which vertex to write first. 2 4 3 1 5

Graphs

Page 2: Graphs - yeasirrayhanprince.github.io · 0 Undirected Graph: Vertices and edges Edges = {02, 05, 04, 15, 14, 24, 23, 45 } No convention Which vertex to write first. 2 4 3 1 5

Undirected Graph: Vertices and edges

No Direction

Representation: 0220

0 2

Page 3: Graphs - yeasirrayhanprince.github.io · 0 Undirected Graph: Vertices and edges Edges = {02, 05, 04, 15, 14, 24, 23, 45 } No convention Which vertex to write first. 2 4 3 1 5

0

Undirected Graph: Vertices and edges

Vertices = {0, 1, 2, 3, 4, 5}2

4

3

1

5

Page 4: Graphs - yeasirrayhanprince.github.io · 0 Undirected Graph: Vertices and edges Edges = {02, 05, 04, 15, 14, 24, 23, 45 } No convention Which vertex to write first. 2 4 3 1 5

0

Undirected Graph: Vertices and edges

Edges = {02, 05, 04, 15, 14, 24, 23, 45 }

No conventionWhich vertex to write first.

2

4

3

1

5

Page 5: Graphs - yeasirrayhanprince.github.io · 0 Undirected Graph: Vertices and edges Edges = {02, 05, 04, 15, 14, 24, 23, 45 } No convention Which vertex to write first. 2 4 3 1 5

0

Directed Graph: Vertices and edges

2

4

3

1

5

0 2

Direction: From Node 0 - Node 2Node 0: Initial VertexNode 2: Terminal VertexDirection: Initial Vertex - Terminal Vertex

Representation: 0220

Page 6: Graphs - yeasirrayhanprince.github.io · 0 Undirected Graph: Vertices and edges Edges = {02, 05, 04, 15, 14, 24, 23, 45 } No convention Which vertex to write first. 2 4 3 1 5

0

Directed Graph: Vertices and edges

2

4

3

1

5

Edges = {05, 04, 02,24, 45, 15, 14, 23}

Page 7: Graphs - yeasirrayhanprince.github.io · 0 Undirected Graph: Vertices and edges Edges = {02, 05, 04, 15, 14, 24, 23, 45 } No convention Which vertex to write first. 2 4 3 1 5

Loops in Graphs

0

4

3

1

5

2

Page 8: Graphs - yeasirrayhanprince.github.io · 0 Undirected Graph: Vertices and edges Edges = {02, 05, 04, 15, 14, 24, 23, 45 } No convention Which vertex to write first. 2 4 3 1 5

Multiple edges in Graphs

0

4

3

1

5

2

Page 9: Graphs - yeasirrayhanprince.github.io · 0 Undirected Graph: Vertices and edges Edges = {02, 05, 04, 15, 14, 24, 23, 45 } No convention Which vertex to write first. 2 4 3 1 5

Adjacency Matrix Representation: Undirected Graph

0 1 2 3 4 5

0

1

2

3

4

5

0

2

4

3

1

5

Page 10: Graphs - yeasirrayhanprince.github.io · 0 Undirected Graph: Vertices and edges Edges = {02, 05, 04, 15, 14, 24, 23, 45 } No convention Which vertex to write first. 2 4 3 1 5

Adjacency Matrix Representation: Undirected Graph

0 1 2 3 4 5

0 0 0 1 0 1 1

1

2

3

4

5

0

2

4

3

1

5

Page 11: Graphs - yeasirrayhanprince.github.io · 0 Undirected Graph: Vertices and edges Edges = {02, 05, 04, 15, 14, 24, 23, 45 } No convention Which vertex to write first. 2 4 3 1 5

Adjacency Matrix Representation: Undirected Graph

0 1 2 3 4 5

0 0 0 1 0 1 1

1 0 0 0 0 1 1

2

3

4

5

0

2

4

3

1

5

Page 12: Graphs - yeasirrayhanprince.github.io · 0 Undirected Graph: Vertices and edges Edges = {02, 05, 04, 15, 14, 24, 23, 45 } No convention Which vertex to write first. 2 4 3 1 5

Adjacency Matrix Representation: Undirected Graph

A 2D array:a[Number of vertices][Number of vertices]

a[i][j] = a[j][i]

0 1 2 3 4 5

0 0 0 1 0 1 1

1 0 0 0 0 1 1

2 1 0 0 1 1 0

3 0 0 1 0 0 0

4 1 1 1 0 0 1

5 1 1 0 0 1 0

Page 13: Graphs - yeasirrayhanprince.github.io · 0 Undirected Graph: Vertices and edges Edges = {02, 05, 04, 15, 14, 24, 23, 45 } No convention Which vertex to write first. 2 4 3 1 5

Adjacency Matrix Representation: Directed Graph

0 1 2 3 4 5

0

1

2

3

4

5

Ini

tial

Vert

ex

Terminal Vertex

0

2

4

3

1

5

Page 14: Graphs - yeasirrayhanprince.github.io · 0 Undirected Graph: Vertices and edges Edges = {02, 05, 04, 15, 14, 24, 23, 45 } No convention Which vertex to write first. 2 4 3 1 5

Adjacency Matrix Representation: Directed Graph

0 1 2 3 4 5

0 0 0 1 0 1 1

1

2

3

4

5

Ini

tial

Vert

ex

Terminal Vertex0

2

4

3

1

5

Page 15: Graphs - yeasirrayhanprince.github.io · 0 Undirected Graph: Vertices and edges Edges = {02, 05, 04, 15, 14, 24, 23, 45 } No convention Which vertex to write first. 2 4 3 1 5

Adjacency Matrix Representation: Directed Graph

0 1 2 3 4 5

0 0 0 1 0 1 1

1 0 0 0 0 1 1

2

3

4

5

Ini

tial

Vert

ex

Terminal Vertex0

2

4

3

1

5

Page 16: Graphs - yeasirrayhanprince.github.io · 0 Undirected Graph: Vertices and edges Edges = {02, 05, 04, 15, 14, 24, 23, 45 } No convention Which vertex to write first. 2 4 3 1 5

Adjacency Matrix Representation: Directed Graph

0 1 2 3 4 5

0 0 0 1 0 1 1

1 0 0 0 0 1 1

2 0 0 0 1 1 0

3

4

5

Ini

tial

Vert

ex

Terminal Vertex

0

2

4

3

1

5

Page 17: Graphs - yeasirrayhanprince.github.io · 0 Undirected Graph: Vertices and edges Edges = {02, 05, 04, 15, 14, 24, 23, 45 } No convention Which vertex to write first. 2 4 3 1 5

Adjacency Matrix Representation: Directed Graph

0 1 2 3 4 5

0 0 0 1 0 1 1

1 0 0 0 0 1 1

2 0 0 0 1 1 0

3 0 0 0 0 0 0

4 0 0 0 0 0 1

5 0 0 0 0 0 0

Ini

tial

Vert

ex

Terminal Vertex

A 2D array:a[Number of vertices][Number of vertices]

a[i][j] = a[j][i]/a[i][j] = a[j][i]No Restriction

Page 18: Graphs - yeasirrayhanprince.github.io · 0 Undirected Graph: Vertices and edges Edges = {02, 05, 04, 15, 14, 24, 23, 45 } No convention Which vertex to write first. 2 4 3 1 5

Degree of a vertex: Undirected Graph

● Number of edges incident with it

● In case of Loop add +2 0

3

1

5

2

4

Page 19: Graphs - yeasirrayhanprince.github.io · 0 Undirected Graph: Vertices and edges Edges = {02, 05, 04, 15, 14, 24, 23, 45 } No convention Which vertex to write first. 2 4 3 1 5

Degree of a vertex: Undirected Graph

● Number of edges incident with it

● In case of Loop add +2

Vertex Degree

0 3

1

2

3

4

5

0

3

1

5

2

4

Page 20: Graphs - yeasirrayhanprince.github.io · 0 Undirected Graph: Vertices and edges Edges = {02, 05, 04, 15, 14, 24, 23, 45 } No convention Which vertex to write first. 2 4 3 1 5

Degree of a vertex: Undirected Graph

● Number of edges incident with it

● In case of Loop add +2

Vertex Degree

0 3

1 2

2

3

4

5

0

3

1

5

2

4

Page 21: Graphs - yeasirrayhanprince.github.io · 0 Undirected Graph: Vertices and edges Edges = {02, 05, 04, 15, 14, 24, 23, 45 } No convention Which vertex to write first. 2 4 3 1 5

Degree of a vertex: Undirected Graph

● Number of edges incident with it

● In case of Loop add +2

Vertex Degree

0 3

1 2

2 3

3 1

4 4

5 3

0

3

1

5

2

4

Page 22: Graphs - yeasirrayhanprince.github.io · 0 Undirected Graph: Vertices and edges Edges = {02, 05, 04, 15, 14, 24, 23, 45 } No convention Which vertex to write first. 2 4 3 1 5

Degree of a vertex: Undirected Graph

● Number of edges incident with it

● In case of Loop add +2

Vertex Degree

0 5 (3 + 2)

1 2

2 3

3 1

4 4

5 3

0

3

1

5

2

4

Page 23: Graphs - yeasirrayhanprince.github.io · 0 Undirected Graph: Vertices and edges Edges = {02, 05, 04, 15, 14, 24, 23, 45 } No convention Which vertex to write first. 2 4 3 1 5

Degree of a vertex: Undirected Graph

Vertex Degree

0 3

1 2

2 3

3 1

4 4

5 3

0 1 2 3 4 5

0 0 0 1 0 1 1

1 0 0 0 0 1 1

2 1 0 0 1 1 0

3 0 0 1 0 0 0

4 1 1 1 0 0 1

5 1 1 0 0 1 0

⅀ (1’s)

⅀ (1’s)

Page 24: Graphs - yeasirrayhanprince.github.io · 0 Undirected Graph: Vertices and edges Edges = {02, 05, 04, 15, 14, 24, 23, 45 } No convention Which vertex to write first. 2 4 3 1 5

Degree of a vertex: Directed Graph

● In Degree : No of INCOMING edges

● Out Degree : No of OUTGOING edges

Page 25: Graphs - yeasirrayhanprince.github.io · 0 Undirected Graph: Vertices and edges Edges = {02, 05, 04, 15, 14, 24, 23, 45 } No convention Which vertex to write first. 2 4 3 1 5

0

Degree of a vertex: Directed Graph

2

4

3

1

5

Vertex In Degree

Out Degree

0 0

1

2

3

4

5

Page 26: Graphs - yeasirrayhanprince.github.io · 0 Undirected Graph: Vertices and edges Edges = {02, 05, 04, 15, 14, 24, 23, 45 } No convention Which vertex to write first. 2 4 3 1 5

0

Degree of a vertex: Directed Graph

2

4

3

1

5

Vertex In Degree

Out Degree

0 0 3

1

2

3

4

5

Page 27: Graphs - yeasirrayhanprince.github.io · 0 Undirected Graph: Vertices and edges Edges = {02, 05, 04, 15, 14, 24, 23, 45 } No convention Which vertex to write first. 2 4 3 1 5

0

Degree of a vertex: Directed Graph

2

4

3

1

5

Vertex In Degree

Out Degree

0 0 3

1

2

3

4 3

5

Page 28: Graphs - yeasirrayhanprince.github.io · 0 Undirected Graph: Vertices and edges Edges = {02, 05, 04, 15, 14, 24, 23, 45 } No convention Which vertex to write first. 2 4 3 1 5

0

Degree of a vertex: Directed Graph

2

4

3

1

5

Vertex In Degree

Out Degree

0 0 3

1

2

3

4 3 1

5

Page 29: Graphs - yeasirrayhanprince.github.io · 0 Undirected Graph: Vertices and edges Edges = {02, 05, 04, 15, 14, 24, 23, 45 } No convention Which vertex to write first. 2 4 3 1 5

Degree of a vertex: Directed Graph

Vertex In Degree

Out Degree

0 0 3

In degree = number of incoming edgesIn degree of vertex 0 = number of incoming edges to 0

0

So, for counting in degrees of vertex 00 must be a TERMINAL VERTEX

Page 30: Graphs - yeasirrayhanprince.github.io · 0 Undirected Graph: Vertices and edges Edges = {02, 05, 04, 15, 14, 24, 23, 45 } No convention Which vertex to write first. 2 4 3 1 5

Degree of a vertex: Directed Graph

Vertex In Degree Out Degree

0 0 30 1 2 3 4 5

0 0 0 1 0 1 1

1 0 0 0 0 1 1

2 0 0 0 1 1 0

3 0 0 0 0 0 0

4 0 0 0 0 0 1

5 0 0 0 0 0 0

⅀(1’s)

Page 31: Graphs - yeasirrayhanprince.github.io · 0 Undirected Graph: Vertices and edges Edges = {02, 05, 04, 15, 14, 24, 23, 45 } No convention Which vertex to write first. 2 4 3 1 5

Degree of a vertex: Directed Graph

Vertex In Degree Out Degree

0 0 3

0 1 2 3 4 5

0 0 0 1 0 1 1

1 0 0 0 0 1 1

2 0 0 0 1 1 0

3 0 0 0 0 0 0

4 0 0 0 0 0 1

5 0 0 0 0 0 0

⅀(1’s)

Page 32: Graphs - yeasirrayhanprince.github.io · 0 Undirected Graph: Vertices and edges Edges = {02, 05, 04, 15, 14, 24, 23, 45 } No convention Which vertex to write first. 2 4 3 1 5

Bipartite Graph

if its vertex set V can be partitioned into two disjoint sets V1 and V2 such

that

no edge in G connects either two vertices in V1 or two vertices in V2

Page 33: Graphs - yeasirrayhanprince.github.io · 0 Undirected Graph: Vertices and edges Edges = {02, 05, 04, 15, 14, 24, 23, 45 } No convention Which vertex to write first. 2 4 3 1 5

Bipartite Graph

V1 = {a, b, d}

V2 = {c, e, f, g}

Page 34: Graphs - yeasirrayhanprince.github.io · 0 Undirected Graph: Vertices and edges Edges = {02, 05, 04, 15, 14, 24, 23, 45 } No convention Which vertex to write first. 2 4 3 1 5

How to decide if a graph is bipartite or not

a

bg

f

e d

c

Page 35: Graphs - yeasirrayhanprince.github.io · 0 Undirected Graph: Vertices and edges Edges = {02, 05, 04, 15, 14, 24, 23, 45 } No convention Which vertex to write first. 2 4 3 1 5

How to decide if a graph is bipartite or not

a

bg

f

e d

c

1. Color any of the vertices blue

Page 36: Graphs - yeasirrayhanprince.github.io · 0 Undirected Graph: Vertices and edges Edges = {02, 05, 04, 15, 14, 24, 23, 45 } No convention Which vertex to write first. 2 4 3 1 5

How to decide if a graph is bipartite or not

a

bg

f

e d

c

2. Identify all uncolored vertices that are adjacent to the blue vertex.

Page 37: Graphs - yeasirrayhanprince.github.io · 0 Undirected Graph: Vertices and edges Edges = {02, 05, 04, 15, 14, 24, 23, 45 } No convention Which vertex to write first. 2 4 3 1 5

How to decide if a graph is bipartite or not

a

bg

f

e d

c

2. Identify all uncolored vertices that are adjacent to the blue vertex. Color them red

Page 38: Graphs - yeasirrayhanprince.github.io · 0 Undirected Graph: Vertices and edges Edges = {02, 05, 04, 15, 14, 24, 23, 45 } No convention Which vertex to write first. 2 4 3 1 5

How to decide if a graph is bipartite or not

a

bg

f

e d

c

3. Identify all uncolored vertices that are adjacent to a red vertex.

Page 39: Graphs - yeasirrayhanprince.github.io · 0 Undirected Graph: Vertices and edges Edges = {02, 05, 04, 15, 14, 24, 23, 45 } No convention Which vertex to write first. 2 4 3 1 5

How to decide if a graph is bipartite or not

a

bg

f

e d

c

3. Identify all uncolored vertices that are adjacent to a red vertex. Color them blue.

Page 40: Graphs - yeasirrayhanprince.github.io · 0 Undirected Graph: Vertices and edges Edges = {02, 05, 04, 15, 14, 24, 23, 45 } No convention Which vertex to write first. 2 4 3 1 5

How to decide if a graph is bipartite or not

a

bg

f

e d

c

Repeat steps 2 and 3 until all the vertices are colored red or blue.

Page 41: Graphs - yeasirrayhanprince.github.io · 0 Undirected Graph: Vertices and edges Edges = {02, 05, 04, 15, 14, 24, 23, 45 } No convention Which vertex to write first. 2 4 3 1 5

How to decide if a graph is bipartite or not

a

bg

f

e d

c

Repeat steps 2 and 3 until all the vertices are colored red or blue.

Page 42: Graphs - yeasirrayhanprince.github.io · 0 Undirected Graph: Vertices and edges Edges = {02, 05, 04, 15, 14, 24, 23, 45 } No convention Which vertex to write first. 2 4 3 1 5

How to decide if a graph is bipartite or not

a

bg

f

e d

c

Repeat steps 2 and 3 until all the vertices are colored red or blue.

Page 43: Graphs - yeasirrayhanprince.github.io · 0 Undirected Graph: Vertices and edges Edges = {02, 05, 04, 15, 14, 24, 23, 45 } No convention Which vertex to write first. 2 4 3 1 5

How to decide if a graph is bipartite or not

a

bg

f

e d

c

If there are any two vertices adjacent of the same color, then your graph is not bipartite, otherwise it is bipartite

∴Bipartite graph

Page 44: Graphs - yeasirrayhanprince.github.io · 0 Undirected Graph: Vertices and edges Edges = {02, 05, 04, 15, 14, 24, 23, 45 } No convention Which vertex to write first. 2 4 3 1 5

How to decide if a graph is bipartite or not

a

bg

f

e d

c

Disjoint setsV1 = {a, b, d}V2 = {c, e, f, g}