Graph Algorithms, 1 - PowerPoint PPT Presentation

About This Presentation
Title:

Graph Algorithms, 1

Description:

Graph Algorithms, 1 Binhai Zhu Computer Science Department, Montana State University – PowerPoint PPT presentation

Number of Views:140
Avg rating:3.0/5.0
Slides: 28
Provided by: monta155
Category:

less

Transcript and Presenter's Notes

Title: Graph Algorithms, 1


1
Graph Algorithms, 1
  • Binhai Zhu
  • Computer Science Department, Montana State
    University

2
Background
  • In many applications, a tree (the most complex
    data structure we have learnt so far) is not
    enough.
  • (1) Think about the network where the vertices
    are all the Internet routers in the world.
  • (2) Think about the human acquaintance
    network, say within MSU-Bozeman.

3
Background
  • In many applications, a tree (the most complex
    data structure we have learnt so far) is not
    enough.
  • (1) Think about the network where the vertices
    are all the Internet routers in the world.
  • (2) Think about the human acquaintance
    network, say within MSU-Bozeman.
  • (3) Again, sometimes we might need a structure
    more complex than a single graph. But for most of
    the applications, a graph should be enough.

4
Definition
  • An (undirected) graph is a pair ltV,Egt.
  • (1) V is the set of vertices, Vn.
  • (2) E is the set of edges, Em.

5
Definition
  • An (undirected) graph is a pair ltV,Egt.
  • (1) V is the set of vertices, Vn.
  • (2) E is the set of edges, Em.
  • (3) So the size of a graph is VE.

6
Definition
  • An (undirected) graph is a pair ltV,Egt.
  • (1) V is the set of vertices, Vn.
  • (2) E is the set of edges, Em.
  • (3) So the size of a graph is VE.

In the graph on our right, n5, m6.
7
Graph Basics,1
In a graph G of n vertices, how many edges could
we have?
8
Graph Basics,1
In a graph G of n vertices, how many edges could
we have? Minimum 0 Maximum n(n-1)/2, how could
that happen?
9
Graph Basics,1
In a graph G of n vertices, how many edges could
we have? Minimum 0 Maximum n(n-1)/2, how could
that happen? When G is a complete graph!
10
Graph Basics,2
The degree of a vertex v in G, deg(v), is the
number of edges incident to v. deg(v1)
3,deg(v2)2 deg(v3)1,deg(v4)2 Can you see some
pattern?
v4
v1
v3
v2
11
Graph Basics,2
The degree of a vertex v in G, deg(v), is the
number of edges incident to v. deg(v1)
3,deg(v2)2 deg(v3)1,deg(v4)2 ?v deg(v) is
even.
v4
v1
v3
v2
12
Graph Basics,2
The degree of a vertex v in G, deg(v), is the
number of edges incident to v. deg(v1)
3,deg(v2)2 deg(v3)1,deg(v4)2 ?v deg(v) is
even. Why?
v4
v1
v3
v2
13
Graph Basics,3
A path in a graph G is a sequence of vertices in
which each successive vertex (starting from the
2nd one) is adjacent to its predecessor in the
path. In a simple path, the vertices and edge are
distinct. A cycle is a simple path with the same
first and final vertex. v1v2v3v4 is not a
path. v3v1v2v4v1 is a path, but not a simple
path. v3v1v2v4 is a simple path (of length
3). v1v2v4v1 is a cycle.
v4
v1
v3
v2
14
Graph Basics,3
A graph G is connected if any two vertices are
connected by some path. A graph which is not
connected consists of a set of connect
components, which are maximal connected
subgraphs. In this graph, we have 2
connected components. Q What is the maximum
number of connected components in any graph?
v5
v4
v1
v6
v3
v2
15
Graph Basics,3
An acyclic connected graph is called a tree. A
set of trees is called a forest. A spanning tree
of a connected graph G is a subgraph containing
all the vertices of G and is itself a tree. Q
What is the number of edges of a tree with n
vertices?
v5
v4
v1
v6
v3
v2
16
Graph Basics,3
An acyclic connected graph is called a tree. A
set of trees is called a forest. A spanning tree
of a connected G is a subgraph containing all the
vertices of G and is itself a tree. An example
of a spanning tree.
v5
v4
v1
v6
v3
v2
17
Graph Basics,3
An acyclic connected graph is called a tree. A
set of trees is called a forest. A spanning tree
of a connected G is a subgraph containing all the
vertices of G and is itself a tree. An example
of a spanning tree.
v5
v4
v1
v6
v3
v2
18
Graph Basics,4
A graph with all possible edges present is called
a complete graph. If it has n vertices, we denote
as Kn. The complement of a given graph G of n
vertices, Gc, is a graph with the same vertices
and an edge is in Gc if and only if it is not in
G. G U Gc ?
v4
v1
v5
v3
v2
19
Graph Basics,4
A graph with all possible edges presented is
called a complete graph. If it has n vertices, we
denote as Kn. The complement of a given graph G
of n vertices, Gc, is a graph with the same
vertices and an edge is in Gc if and only if it
is not in G. G U Gc Kn
v4
v1
v5
v3
v2
GC is in yellow
20
Graph Representation
A graph is a data structure, so we need to
consider how to represent it. Idea?
v4
v1
v5
v3
v2
21
Graph Representation
A graph is a data structure, so we need to
consider how to represent it. Adjacency matrix
aij 1 if vi is adjacent to vj
v4
v1
v5
v3
v2
22
Graph Representation
A graph is a data structure, so we need to
consider how to represent it. Adjacency matrix
aij 1 if vi is adjacent to vj otherwise it is
0.
v4
v5
v4
v3
v2
v1
1
0
0
1
1
v1
v1
v5
0
0
0
1
1
v2
v3
1
0
0
0
1
v2
v3
v4
1
1
0
0
0
0
0
1
v5
0
0
23
Graph Representation
A graph is a data structure, so we need to
consider how to represent it. Adjacency matrix
aij 1 if vi is adjacent to vj otherwise it is
0.
v4
v5
v4
v3
v2
v1
1
0
0
1
1
v1
v1
v5
0
0
0
1
1
v2
v3
1
0
0
0
1
v2
v3
v4
1
1
0
0
0
What property does this matrix have?
0
0
1
v5
0
0
24
Graph Representation
A graph is a data structure, so we need to
consider how to represent it. Adjacency matrix
aij 1 if vi is adjacent to vj otherwise it is
0.
v4
v5
v4
v3
v2
v1
1
0
0
1
1
v1
v1
v5
0
0
0
1
1
v2
v3
1
0
0
0
1
v2
v3
v4
1
1
0
0
0
What is the cost of this representation?
0
0
1
v5
0
0
25
Graph Representation
A graph is a data structure, so we need to
consider how to represent it. Adjacency list for
each vi maintain the list of vertices incident to
vi.
v4
v1
v4
v2
v3
v1
v5
v2
v3
v2
v3
v1
v5
v4
v5
26
Graph Representation
A graph is a data structure, so we need to
consider how to represent it. Adjacency list for
each vi maintain the list of vertices incident to
vi.
v4
v1
v4
v2
v3
v1
v5
v2
v3
v2
v3
v1
v5
v4
What is the cost of this representation?
v5
27
Graph Representation
A graph is a data structure, so we need to
consider how to represent it. Adjacency list for
each vi maintain the list of vertices incident to
vi.
v4
v1
v4
v2
v3
v1
v5
v2
v3
v2
v3
v1
v5
v4
What is the cost of this representation?
O(VE)O(nm)
v5
Write a Comment
User Comments (0)
About PowerShow.com