Docsity

Graph theory and trees, Lecture notes of Computer Science

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