Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Graph theory and trees, Lecture notes of Computer Science

Pdf for graph theory and trees useful for btech students in every college

Typology: Lecture notes

2018/2019

Uploaded on 12/01/2019

naga-lokesh-paipuri
naga-lokesh-paipuri 🇮🇳

1 document

1 / 34

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Section 1.4 CS340-Discrete Structures Page 1
Section 1.4: Graphs and Trees
A graph is a set of objects (called vertices or nodes) and edges
between pairs of nodes.
Vertices = {Ve, G, S, F, Br, Co, Eq, Pe, Bo,Pa, Ch, A, U}
Edges = { {Ve,G}, {Ve,Br}, … }
S
G
U
Br
Ve
Co
Eq
Ch
Pa
Bo
Pe
F
A
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22

Partial preview of the text

Download Graph theory and trees and more Lecture notes Computer Science in PDF only on Docsity!

Section 1.4: Graphs and Trees A graph is a set of objects (called vertices or nodes ) and edges between pairs of nodes. Vertices = {Ve, G, S, F, Br, Co, Eq, Pe, Bo,Pa, Ch, A, U} Edges = { {Ve,G}, {Ve,Br}, … }

S

G

U

Br Ve Co Eq Ch Pa Bo Pe

F

A

A path from vertex x 0 to xn is a sequence of edges x 0 , x 1 , …, xn, where there is an edge from xi-1 to xi for 1≤i≤n. The length of a path is the number of edges in it. A cycle is a path that begins and ends at the same vertex and has no repeated edges.

S

G

U

Br Ve Co Eq Ch Pa Bo Pe

F

A

A path from Pe to Br

The sequence Co , Br , G , Ve , Co is a cycle. The sequence S , F , S is not a cycle, since edge { S , F } occurs twice. In-class quiz: What is the longest path from Bo to F with distinct edges and no cylces?

S

G

U

Br Ve Co Eq Ch Pa Bo Pe

F

A

The sequence Co , Br , G , Ve , Co is a cycle. The sequence S , F , S is not a cycle, since edge { S , F } occurs twice. In-class quiz: What is the longest path from Bo to F with distinct edges and no cylces? A graph is n-colorable if its vertices can be colored using n different colors such that adjacent vertices have different colors. The chromatic number of a graph is the smallest such n. In-class quiz: What is the chromatic color of this graph? i.e., how many colors does it take to color this graph?

S

G

U

Br Ve Co Eq Ch Pa Bo Pe

F

A

Graph Traversals

A graph traversal starts at some vertex v and visits all vertices without visiting any vertex more than once. (We assume connectedness: all vertices are reachable from v.) Breadth-First Traversal

  • First visit v.
  • Then visit all vertices reachable from v with a path length of 1.
  • Then visit all vertices reachable from v with a path length of 2. (… not already visited earlier)
  • And so on.

S

G

U

Br Ve Co Eq Ch Pa Bo Pe

F

A

Graph Traversals

A graph traversal starts at some vertex v and visits all vertices without visiting any vertex more than once. (We assume connectedness: all vertices are reachable from v.) Breadth-First Traversal

  • First visit v.
  • Then visit all vertices reachable from v with a path length of 1.
  • Then visit all vertices reachable from v with a path length of 2. (… not already visited earlier)
  • And so on. Example: v=Bo Bo

S

G

U

Br Ve Co Eq Ch Pa Bo Pe

F

A

Graph Traversals

A graph traversal starts at some vertex v and visits all vertices without visiting any vertex more than once. (We assume connectedness: all vertices are reachable from v.) Breadth-First Traversal

  • First visit v.
  • Then visit all vertices reachable from v with a path length of 1.
  • Then visit all vertices reachable from v with a path length of 2. (… not already visited earlier)
  • And so on. Example: v=Bo Bo,Pe,Br,Pa,A,Ch,U,Eq,Ve,S,G,F,Co

S

G

U

Br Ve Co Eq Ch Pa Bo Pe

F

A

In-Class Quiz: Find a breadth-first traversal starting with F. A D

C

E

G

F

H

B

In-Class Quiz: Find a breadth-first traversal starting with F. One answer: F,H,D,G,B,A,E,C In-Class Quiz: Find a breadth-first traversal starting with C. One answer: C,A,E,D,F,B,H,G A D

C

E

G

F

H

B

Depth-First Traversal Start with a vertex v and visit all reachable vertices. Start by going as far as you can. Then backup a little and go down another path as far as possible. Only backup as far as necessary, then try the next path. Example: Start at Ch. Ch S

G

U

Br Ve Co Eq Ch Pa Bo Pe

F

A

Depth-First Traversal Start with a vertex v and visit all reachable vertices. Start by going as far as you can. Then backup a little and go down another path as far as possible. Only backup as far as necessary, then try the next path. Example: Start at Ch. Ch,Pe,Co,Ve,G,S,F,Br,Eq S

G

U

Br Ve Co Eq Ch Pa Bo Pe

F

A

Depth-First Traversal Start with a vertex v and visit all reachable vertices. Start by going as far as you can. Then backup a little and go down another path as far as possible. Only backup as far as necessary, then try the next path. Example: Start at Ch. Ch,Pe,Co,Ve,G,S,F,Br,Eq,A,U S

G

U

Br Ve Co Eq Ch Pa Bo Pe

F

A

In-Class Quiz: Find a depth-first traversal starting a F. A D

C

E

G

F

H

B

In-Class Quiz: Find a depth-first traversal starting a F. One Answer: F,H,G,D,B,A,C,E In-Class Quiz: Find a depth-first traversal starting a E. A D

C

E

G

F

H

B