graph theory and traversals - university of british...

22
Graph Theory and Traversals Our final ADT...

Upload: lytruc

Post on 24-Apr-2019

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Graph Theory and Traversals - University of British Columbiacs221/2008S1/notes/set10_graphtheory.pdf · A simple graph is a graph that has at most one edge between any two vertices

Graph Theory and TraversalsOur final ADT...

Page 2: Graph Theory and Traversals - University of British Columbiacs221/2008S1/notes/set10_graphtheory.pdf · A simple graph is a graph that has at most one edge between any two vertices

Learning Goals

After this unit, you should be able to...• Describe the properties and possible applications of various kinds of

graphs (e.g., simple, complete), and the relationships among vertices, edges, and degrees.

• Prove basic theorems about simple graphs (e.g. handshaking theorem).• Convert between adjacency matrices/lists and their corresponding

graphs.• Determine whether two graphs are isomorphic.• Determine whether a given graph is a subgraph of another.• Perform breadth-first and depth-first searches in graphs.• Explain why graph traversals are more complicated than tree traversals.

Page 3: Graph Theory and Traversals - University of British Columbiacs221/2008S1/notes/set10_graphtheory.pdf · A simple graph is a graph that has at most one edge between any two vertices

An Introduction to Graph Theory

A graph G=(V,E) consists of: (a) a non-empty set of vertices V, and (b) a set of edges E between pairs of those vertices. An edge e ∈ E ⊆ V×V, where e = (u,v) and u,v ∈ V.

G = <V,E>

V = vertices = {A,B,C,D,E,F,G,H,I,J,K,L}

E = edges = {(A,B),(B,C),(C,D),(D,E), (E,F),(F,G),(G,H),(H,A),(A,J),(A,G),(B,J),(K,F),(C,L),(C,I),(D,I),(D,F),(F,I),(G,K),(J,L),(J,K),(K,L),(L,I)}

Page 4: Graph Theory and Traversals - University of British Columbiacs221/2008S1/notes/set10_graphtheory.pdf · A simple graph is a graph that has at most one edge between any two vertices

Examples of Graphs

•Computer networks

•Vertices are:

•Edges are:

•Web sites

•Vertices are:

•Edges are:

•Cities and highways

•Vertices are:

•Edges are:

Page 5: Graph Theory and Traversals - University of British Columbiacs221/2008S1/notes/set10_graphtheory.pdf · A simple graph is a graph that has at most one edge between any two vertices

Examples of Graphs (cont.):

•Circuit layouts

•Acquaintance graphs

•Precedence (prerequisite) graphs

•Telephone call graphs

•Pipelines, plumbing, electrical wiring, etc.

•Meshes

Page 6: Graph Theory and Traversals - University of British Columbiacs221/2008S1/notes/set10_graphtheory.pdf · A simple graph is a graph that has at most one edge between any two vertices

We say that edge e is incident on vertices u and v. Furthermore, u and v are said to be adjacent vertices, and u and v are the endpoints of edge e.

Graphs may be directed (top right)or undirected (bottom right).

In an undirected graph, (u,v) = (v,u).

Some authors write {u,v} instead of (u,v) to indicate explicitly that the vertices areunordered.

In a directed graph, however, the orderedpair (u,v) implies that the edge goes from u to v.

Page 7: Graph Theory and Traversals - University of British Columbiacs221/2008S1/notes/set10_graphtheory.pdf · A simple graph is a graph that has at most one edge between any two vertices

A simple graph is a graph that has at most one edge between any two vertices. Loops (also called self-loops, i.e., where an edge goes from one vertex to itself) are not allowed.

Note: A simple graph may be directed or undirected. We’ll assume it’s undirected, unless explicitly stated otherwise.

To represent a graph, we usually store:

a) Vertex information—stored as an array or list.

b) Edge (connectivity) information—stored as an adjacency matrix.

Page 8: Graph Theory and Traversals - University of British Columbiacs221/2008S1/notes/set10_graphtheory.pdf · A simple graph is a graph that has at most one edge between any two vertices

An n×n adjacency matrix A can be used to represent a graph with n vertices v1, v2, … vn. The entry in row i and column j of A is the number of edges (vi, vj) in the graph.

The adjacency matrix of a simple graph contains only 0’s and 1’s.

The adjacency matrix of an undirected graph is symmetric.

For graphs with few edges, the adjacency matrix is sparse (i.e., contains mostly zeros).

v1

v2

v3

v4v5

v6

v1 v2 v3 v4 v5 v6

v1 1 1

v2 1 1 1

v3 1 1 1 1

v4 1 1 1 1

v5 1 1

v6 1 1 1

Page 9: Graph Theory and Traversals - University of British Columbiacs221/2008S1/notes/set10_graphtheory.pdf · A simple graph is a graph that has at most one edge between any two vertices

Some Common Graphs:

A cycle Cn has n vertices {v1, v2, … , vn} and n edges {(v1,v2), (v2,v3), …, (vn,v1)}.

C3 C5

A wheel Wn is the cycle Cn , n ≥ 3, with an extra vertex, and this extra vertex is connected to all of the other n vertices.

W3 W5

Page 10: Graph Theory and Traversals - University of British Columbiacs221/2008S1/notes/set10_graphtheory.pdf · A simple graph is a graph that has at most one edge between any two vertices

An n-cube (or hypercube) is an n-dimensional cube, denoted by Qn , n ≥ 0. This graph has vertices representing the 2n bit strings of length n. Two vertices are adjacent iff the bit strings that they represent differ in exactly one bit position.

Q1 Q2

Q3 Q4

Page 11: Graph Theory and Traversals - University of British Columbiacs221/2008S1/notes/set10_graphtheory.pdf · A simple graph is a graph that has at most one edge between any two vertices

A complete graph on n vertices, denoted Kn, is a simple graph with an edge between each pair of vertices. Examples:

K4 K6

A simplex is a complete graph of size n embedded in Rm (m > n) … and we’re usually interested in cases where n ∈{1, 2, 3, 4}.

Page 12: Graph Theory and Traversals - University of British Columbiacs221/2008S1/notes/set10_graphtheory.pdf · A simple graph is a graph that has at most one edge between any two vertices

Paths and Cycles

Suppose G = (V, E) is a graph.

•A path of length k from a vertex u to a vertex v is a sequence u=v0 , v1 , v2 , … , vk=v of vertices such that (vi-1, vi) ∈ E, and there are no repeated edges. Repeated vertices are allowed.

If there are parallel edges in the graph, then the sequence must specify, for each i, which of the edges (vi-1, vi) is in the path.

•A simple path is a path with no repeated vertices (and therefore no repeated edges).

•A cycle (or circuit) is a path from v ∈ V to itself, with at least one edge. The single-edge case is called a loop.

•A simple cycle is a cycle with no repeated vertices except for the first and the last vertex.

Note: Different authors may use different definitions.

Page 13: Graph Theory and Traversals - University of British Columbiacs221/2008S1/notes/set10_graphtheory.pdf · A simple graph is a graph that has at most one edge between any two vertices

Graph Traversals

Why might we want to traverse a graph?

•To find the shortest path between two vertices: u and v

•To find all vertices within radius r of a given vertex v

•To traverse (visit, search) all vertices in some order:

•Breadth First

•Depth First

Page 14: Graph Theory and Traversals - University of British Columbiacs221/2008S1/notes/set10_graphtheory.pdf · A simple graph is a graph that has at most one edge between any two vertices

BFS: Breadth-First Search (or Traversal) in Trees

In a breadth-first traversal, we visit the vertices (nodes) in level-wise order. Example: 6, 3, 7, 2, 5, 9.

BFS Algorithm (for Trees)•Add root to queue •While queue not empty

•Dequeue node N from queue•Print/Process N. (If in search mode, return info. when found, and terminate the algorithm.)•Enqueue N’s children to queue

63 7

2 5 9

Page 15: Graph Theory and Traversals - University of British Columbiacs221/2008S1/notes/set10_graphtheory.pdf · A simple graph is a graph that has at most one edge between any two vertices

Breadth-First Search (or Traversal) in Graphs (Nyhoff pp. 896-897 provides examples on DFS in directed graphs)

BFS Algorithm (for Graphs)•Add selected starting vertex to queue •While queue not empty

•Dequeue vertex V from queue•Print/Process V. (If in search mode, return info. when found, and terminate the algorithm.)•Enqueue all unvisited neighbours of V to queue

Note 1: We need to mark each vertex as being visited (so far), or not.

Note 2: For a directed graph, u is a neighbour of v if (v, u) is an edge.

Page 16: Graph Theory and Traversals - University of British Columbiacs221/2008S1/notes/set10_graphtheory.pdf · A simple graph is a graph that has at most one edge between any two vertices

Separate Tree and Graph Examples: Cities in Germany

Page 17: Graph Theory and Traversals - University of British Columbiacs221/2008S1/notes/set10_graphtheory.pdf · A simple graph is a graph that has at most one edge between any two vertices

DFS: Depth-First Search (or Traversal) in Trees (Nyhoff pp. 892-893)

In a depth-first traversal, we visit the vertices (nodes) in an order somewhat similar to preorder traversal. Example: 6, 3, 2, 5, 7, 9.

DFS Algorithm (for Trees):

call: DFS(root)DFS(node)

•Print/process node. (In search mode, return info. when found, and terminate the algorithm.)•For each child c of the node:

•DFS(c)

63 7

2 5 9

Page 18: Graph Theory and Traversals - University of British Columbiacs221/2008S1/notes/set10_graphtheory.pdf · A simple graph is a graph that has at most one edge between any two vertices

Depth-First Search in Graphs (Nyhoff pp. 894-896)

DFS Algorithm (for Graphs):DFS(selected starting vertex V)

DFS(V)•Print/process V. (If in search mode, return info. when found, and terminate the algorithm.)•For each unvisited neighbour c of V:

•DFS(c)

Note: We need to mark each vertex as being visited (so far), or not.

Page 19: Graph Theory and Traversals - University of British Columbiacs221/2008S1/notes/set10_graphtheory.pdf · A simple graph is a graph that has at most one edge between any two vertices

The degree of a vertex v∈V is denoted deg(v) and represents the number of edges incident on v. Note that a loop at v contributes 2 towards the degree.

Handshaking Theorem: If G=(V,E) is an undirected graph, then:

Proof:

Corollary: An undirected graph has an even number of vertices of odd degree.

Proof:

Page 20: Graph Theory and Traversals - University of British Columbiacs221/2008S1/notes/set10_graphtheory.pdf · A simple graph is a graph that has at most one edge between any two vertices

In a directed graph, the in-degree of v ∈V, denoted deg– (v) is the number of edges coming into v. The out-degree, denoted, deg+(v) is the number of edges coming out of v. Furthermore, deg(v) = deg– (v) + deg+ (v), and:

Page 21: Graph Theory and Traversals - University of British Columbiacs221/2008S1/notes/set10_graphtheory.pdf · A simple graph is a graph that has at most one edge between any two vertices

A graph G=(V,E) is connected if for all vertices u,v ∈V, there is a path from u to v. Conversely, an unconnected graph has more than one “piece”, and for at least one pair of vertices, there will be no path between them. The “pieces” themselves are called connected components or connected subgraphs. In general:

A subgraph of a graph G=(V,E) is a graph H=(W,F) where W ⊆ V and F ⊆ E. (Note about the trivial case: a subgraph can be unconnected (i.e., a single vertex).

Page 22: Graph Theory and Traversals - University of British Columbiacs221/2008S1/notes/set10_graphtheory.pdf · A simple graph is a graph that has at most one edge between any two vertices

Learning Goals

After this unit, you should be able to...• Describe the properties and possible applications of various kinds of

graphs (e.g., simple, complete), and the relationships among vertices, edges, and degrees.

• Prove basic theorems about simple graphs (e.g. handshaking theorem).• Convert between adjacency matrices/lists and their corresponding

graphs.• Determine whether two graphs are isomorphic.• Determine whether a given graph is a subgraph of another.• Perform breadth-first and depth-first searches in graphs.• Explain why graph traversals are more complicated than tree traversals.