Docsity

Graph Representations and Types: Lecture Notes for Comp 590/Comp 790-90, Fall 2008 - Prof., Study notes of Computer Science

Toggle sidebar
bg1
10/30/2008 Comp 590/Comp 790-90 Fall 2008 1
Lecture 18:
Lecture 18:
Graph Representations
Graph Representations
Not in book
Not in book
10/30/2008 Comp 590/Comp 790-90 Fall 2008 2
What is a Graph?
What is a Graph?
•Representation of data and relationships
•Points connected by lines
•The points are called vertices, the lines edges
•A graph is defined by two sets G = {V,E}
•Where V is a set of vertices, e.g. V= {a, b, c, d, e}
•E is a set of edges given by 2-tuples, e.g.
E = {(a, c), (a, d), (b, d), (b, e), (c, e), (d, c)}
a
d
be
c
pf3
pf4
pf5
pf8
pf9
pfa
pfd

Partial preview of the text

Download Graph Representations and Types: Lecture Notes for Comp 590/Comp 790-90, Fall 2008 - Prof. and more Study notes Computer Science in PDF only on Docsity!

10/30/2008 Comp 590/Comp 790-90 Fall 2008 1

Lecture 18:Lecture 18:

Graph RepresentationsGraph Representations

Not in bookNot in book

10/30/2008 Comp 590/Comp 790-90 Fall 2008 2

What is a Graph? What is a Graph?

  • Representation of data and relationships
  • Points connected by lines
  • The points are called vertices , the lines edges
  • A graph is defined by two sets G = {V,E}
  • Where V is a set of vertices, e.g. V = { a, b, c, d, e }
  • E is a set of edges given by 2-tuples, e.g.

E = { (a, c), (a, d), (b, d), (b, e), (c, e), (d, c) }

a d

e b c

10/30/2008 Comp 590/Comp 790-90 Fall 2008 3

Symbolic Representation Symbolic Representation

• A drawing of a graph is a useful visual aid,

but a graph is not a drawing

• Identical graphs can be drawn differently

• Complicated graphs can be difficult to draw

• We’ll focus on how graphs are represented in a

computer, and how to write graph algorithms

a d

e b c

a d e b

≡ c

10/30/2008 Comp 590/Comp 790-90 Fall 2008 4

Special Types Graphs Special Types Graphs

• Directed Graphs (digraph)

  • Graph in which each edge has a direction
  • Vertices with only outgoing edges arecalled “sources” and edges with only

incoming edges are called “sinks”

  • Digraph paths that repeat verticesare called “cycles”

• Directed Acyclic Graphs (DAGs)

  • A digraph containing no cycles

a b c d e

source

sink

Comp 590/Comp 790-90 Fall 2008

Path Finding Path Finding

  • Path between “a” and “h”.

Path length is 20.

a

b

c

h g f

d e

Path Finding Path Finding

  • A second path from “a” to “h”

Path length is 28

  • What is the shortest path?
  • Is there a path through all edges?
  • What is the minimal cost path through allvertices?

a

b

c

h g f

d e

Comp 590/Comp 790-90 Fall 2008

Example Of No Path Example Of No Path

No path between a and h.

a

b

c

h g f

d e

Connected Graph Connected Graph

  • Undirected graph.
  • There is a path between every pair of vertices.

a

b

c

h g f

d e

Comp 590/Comp 790-90 Fall 2008

Cycles And Connectedness Cycles And Connectedness

  • Removal of an edge that is on a cycle does notaffect connectedness.

a

b

c

h g f

d e

Tree Tree

  • Connected graph that has no cycles.
  • n vertex connected graph with n-1 edges.

a

b

c

h g f

d e

a

b

c

h g f

d e

Comp 590/Comp 790-90 Fall 2008

Spanning Tree Spanning Tree

  • Subgraph that includes all vertices of the

original graph.

  • Subgraph is a tree.

ƒ If original graph has n vertices, the spanning tree hasn vertices and n-1 edges.

a

b

c

h g f

d e

Minimum Cost Spanning Tree Minimum Cost Spanning Tree

  • Tree cost is sum of edge weights/costs.

a

b

c

h g f

d e

  • Reduce graph to tree of smallest cost

Comp 590/Comp 790-90 Fall 2008

Adjacency Matrix Adjacency Matrix

  • Binary (1/0) n x n matrix, where n = # of vertices• A(i,j) = 1 iff (i,j) is an edge

Adjacency Matrix Properties Adjacency Matrix Properties

•Diagonal entries are zero.^0 1 1 1 •Adjacency matrix of an undirected graph is symmetric. A(i,j) = A(j,i) for all i and j.

Comp 590/Comp 790-90 Fall 2008

Adjacency Matrix (Digraph) Adjacency Matrix (Digraph)

•Diagonal entries are zero. (If self-edges are disallowed) •Adjacency matrix of a digraph need not be symmetric.

Adjacency Matrix Adjacency Matrix

  • n^2 bits of space
  • For an undirected graph, may store only lower

or upper triangle (exclude diagonal).

ƒ (n-1)n/2 bits

  • O(n) time to find vertex degree and/or the set of

all vertices adjacent to a given vertex.

  • Constant time to test if an edge exists

Comp 590/Comp 790-90 Fall 2008

Weighted Graphs Weighted Graphs

  • Cost adjacency matrix.

Stores the edge weight in the adjacency matrix

ƒ C(i,j) = cost of edge (i,j)

a b c d e

4 de 00 00 02 00 04

c 0 0 0 0 1

b 0 0 4 1 0

a 0 2 0 5 0

G a b c d e

Weighted Graphs Weighted Graphs

  • Adjacency lists =>

each list element is a pair

(adjacent vertex, edge weight)

  • G = { ‘a’: [(‘b’,2), (‘d’,5)],

‘b’:[(‘c’,4),(‘d’,1)],

‘c’:[(‘e’,1)],

‘d’:[(‘c’,2),(‘e’,4)],

‘e’:[]}

a b c d e