what is a graph? what is a graph? directed vs. undirected graphs directed vs. undirected graphs ...

19
Graphs

Upload: morris-short

Post on 01-Jan-2016

233 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: What is a graph? What is a graph?  Directed vs. undirected graphs Directed vs. undirected graphs  Trees vs graphs Trees vs graphs  Terminology: Degree

Graphs

Page 2: What is a graph? What is a graph?  Directed vs. undirected graphs Directed vs. undirected graphs  Trees vs graphs Trees vs graphs  Terminology: Degree

What is a graph? Directed vs. undirected graphs Trees vs graphs Terminology: Degree of a Vertex Graph terminology Graph Traversal Graph representation

Topics to be discussed…

Page 3: What is a graph? What is a graph?  Directed vs. undirected graphs Directed vs. undirected graphs  Trees vs graphs Trees vs graphs  Terminology: Degree

What is a graph? A data structure that consists of a set of nodes (vertices) and a set of

edges that relate the nodes to each other

The set of edges describes relationships among the vertices

Page 4: What is a graph? What is a graph?  Directed vs. undirected graphs Directed vs. undirected graphs  Trees vs graphs Trees vs graphs  Terminology: Degree

Formal definition of graphs A graph G is defined as follows:

G=(V,E)

V(G): a finite, nonempty set of vertices

E(G): a set of edges (pairs of vertices)

back

Page 5: What is a graph? What is a graph?  Directed vs. undirected graphs Directed vs. undirected graphs  Trees vs graphs Trees vs graphs  Terminology: Degree

Directed vs. undirected graphs When the edges in a graph have no direction, the graph is called

undirected

Page 6: What is a graph? What is a graph?  Directed vs. undirected graphs Directed vs. undirected graphs  Trees vs graphs Trees vs graphs  Terminology: Degree

Directed vs. undirected graphs (cont.) When the edges in a graph have a direction, the graph is called

directed (or digraph)

E(Graph2) = {(1,3) (3,1) (5,9) (9,11) (5,7)

Warning: if the graph is directed, the order of the vertices in each edge is important !!

back

Page 7: What is a graph? What is a graph?  Directed vs. undirected graphs Directed vs. undirected graphs  Trees vs graphs Trees vs graphs  Terminology: Degree

Trees vs graphs

Trees are special cases of graphs!!

back

Page 8: What is a graph? What is a graph?  Directed vs. undirected graphs Directed vs. undirected graphs  Trees vs graphs Trees vs graphs  Terminology: Degree

The degree of a vertex is the number of edges incident to that vertex

For directed graph, the in-degree of a vertex v is the number of edgesthat have v as the headthe out-degree of a vertex v is the number of edgesthat have v as the tailif di is the degree of a vertex i in a graph G with n vertices and e edges, the number of edges is

Terminology:Degree of a Vertex

Page 9: What is a graph? What is a graph?  Directed vs. undirected graphs Directed vs. undirected graphs  Trees vs graphs Trees vs graphs  Terminology: Degree

0

1 2

3 4 5 6

G1 G2

3 2

3 3

1 1 1 1

directed graphin-degreeout-degree

0

1

2

G3

in:1, out: 1

in: 1, out: 2

in: 1, out: 0

0

1 2

3

33

3

Examples

back

Page 10: What is a graph? What is a graph?  Directed vs. undirected graphs Directed vs. undirected graphs  Trees vs graphs Trees vs graphs  Terminology: Degree

Graph terminology Adjacent nodes: two nodes are adjacent if they are connected by an edge

Path: a sequence of vertices that connect two nodes in a graph Complete graph: a graph in which every vertex is directly connected to every

other vertex

Page 11: What is a graph? What is a graph?  Directed vs. undirected graphs Directed vs. undirected graphs  Trees vs graphs Trees vs graphs  Terminology: Degree

Graph terminology (cont.) What is the number of edges in a complete undirected graph with N

vertices? 

N * (N-1) / 2

Page 12: What is a graph? What is a graph?  Directed vs. undirected graphs Directed vs. undirected graphs  Trees vs graphs Trees vs graphs  Terminology: Degree

Graph terminology (cont.) Weighted graph: a graph in which each edge carries a value

back

Page 13: What is a graph? What is a graph?  Directed vs. undirected graphs Directed vs. undirected graphs  Trees vs graphs Trees vs graphs  Terminology: Degree

Graph Traversal

Problem: Search for a certain node or traverse all nodes in the graph Depth First Search

◦ Once a possible path is found, continue the search until the end of the path

Breadth First Search◦ Start several paths at a time, and advance in each one step at a time

back

Page 14: What is a graph? What is a graph?  Directed vs. undirected graphs Directed vs. undirected graphs  Trees vs graphs Trees vs graphs  Terminology: Degree

Graph Representations

Adjacency MatrixAdjacency Lists

Page 15: What is a graph? What is a graph?  Directed vs. undirected graphs Directed vs. undirected graphs  Trees vs graphs Trees vs graphs  Terminology: Degree

Adjacency MatrixLet G=(V,E) be a graph with n vertices.

The adjacency matrix of G is a two-dimensional n by n array, say adj_mat

If the edge (vi, vj) is in E(G), adj_mat[i][j]=1

If there is no such edge in E(G), adj_mat[i][j]=0

The adjacency matrix for an undirected graph is symmetric; the adjacency matrix for a digraph need not be symmetric

Page 16: What is a graph? What is a graph?  Directed vs. undirected graphs Directed vs. undirected graphs  Trees vs graphs Trees vs graphs  Terminology: Degree

0

1

1

1

1

0

1

1

1

1

0

1

1

1

1

0

0

1

0

1

0

0

0

1

0

0

1

1

0

0

0

0

0

1

0

0

1

0

0

0

0

1

0

0

1

0

0

0

0

0

1

1

0

0

0

0

0

0

0

0

0

0

1

0

0

0

0

0

0

1

0

1

0

0

0

0

0

0

1

0

1

0

0

0

0

0

0

1

0

G1G2

G4

0

1 2

3

0

1

2

1

0

23

4

56

7

symmetric

undirected: n2/2directed: n2

back

Page 17: What is a graph? What is a graph?  Directed vs. undirected graphs Directed vs. undirected graphs  Trees vs graphs Trees vs graphs  Terminology: Degree

Adjacency Lists (data structures)

Each row in adjacency matrix is represented as an adjacency list.

Page 18: What is a graph? What is a graph?  Directed vs. undirected graphs Directed vs. undirected graphs  Trees vs graphs Trees vs graphs  Terminology: Degree

0123

012

01234567

1 2 30 2 30 1 30 1 2

G1

10 2

G3

1 20 30 31 254 65 76G4

0

1 2

3

0

1

2

1

0

2

3

4

5

6

7

An undirected graph with n vertices and e edges ==> n head nodes and 2e list nodesback

Page 19: What is a graph? What is a graph?  Directed vs. undirected graphs Directed vs. undirected graphs  Trees vs graphs Trees vs graphs  Terminology: Degree

Thank You