Graphs - SIUE Computer Science

advertisement
MATH 224 – Discrete Mathematics
Graphs
Formally a graph is just a collection of unordered or ordered pairs, where for example,
if {a,b} G if a, b  V. Here G is the graph and V is called the set of nodes.
So a graph G(V, E) is a set of objects V (of any type), which are called nodes or
sometimes vertices. And E is actually the set of pairs, which are called edges or
sometimes called links.
The edges may be unordered pairs {a,b} or ordered pairs (a,b). With unordered pairs
{a,b} and {b, a} are equivalent. But with ordered pairs the order matters so (a,b) is not
the same as (b,a).
4/13/2015
1
MATH 224 – Discrete Mathematics
Undirected Graph
Edges = {{0,2}, {1,2}, {1,3}, {1,4}, {2,4}, {2,5}, …}
4/13/2015
0
1
2
3
4
5
2
MATH 224 – Discrete Mathematics
Directed Graph
Edges = {(0,2), (1,2), (3,1), (1,4), (2,4), (4,2), …}
4/13/2015
0
1
2
3
4
5
3
MATH 224 – Discrete Mathematics
Simple Graphs
Multiple edges between pairs of nodes and self loops are not allowed in
simple graphs.
0
1
2
Multiple
Edges
Self Loop
3
4/13/2015
4
5
4
MATH 224 – Discrete Mathematics
Paths
Paths are a sequence of nodes, e.g., 0, 2, 5, 3, where no node is repeated
and consecutive nodes correspond to an edge.
4/13/2015
0
1
2
3
4
5
5
MATH 224 – Discrete Mathematics
Not a Path (called a walk)
Note that some nodes are repeated: 1, 2, 4, 1, 3, 5
4/13/2015
0
1
2
3
4
5
6
MATH 224 – Discrete Mathematics
Cycle
A cycle is a sequence of nodes, e.g., 1, 2, 4, 5, 3, 1, where consecutive nodes
correspond to an edge and the only node that is repeated is the first and last
node.
4/13/2015
0
1
2
3
4
5
7
MATH 224 – Discrete Mathematics
Cycle Graph
A cycle graph consists of a single cycle.
1
2
0
3
5
4/13/2015
4
8
MATH 224 – Discrete Mathematics
Trees
Trees are acyclic (no cycles) graphs that are connected (a path between
every pair of edges). In an N node tree, how many edges are there?
4/13/2015
0
1
2
3
4
5
9
MATH 224 – Discrete Mathematics
Not a Tree
0
1
2
3
4
5
Cycle
4/13/2015
10
MATH 224 – Discrete Mathematics
Not a Tree
This graph is not a tree because it has a cycle and is not connected. There is no path
between 0 and 4 for example. What other pairs of nodes are not connected?
0
1
2
3
4
5
Cycle
4/13/2015
11
MATH 224 – Discrete Mathematics
Not a Tree
A graph with two components. Two subgraphs that are connected. What are
the nodes in each component?
0
1
2
4
5
6
3
4/13/2015
12
MATH 224 – Discrete Mathematics
Isomorphic Graphs
Two graphs are called isomorphic if they are the same if renaming the nodes will
result in identical graphs.
Vertex 1 Corresponds to Vertex 4
0
1
2
0
1
2
3
4
5
3
4
5
4/13/2015
13
MATH 224 – Discrete Mathematics
Isomorphic Graphs ?
Are these two graphs isomorphic?
1
0
3
4
Graph A
4/13/2015
2
5
1
0
3
4
2
5
Graph B
14
MATH 224 – Discrete Mathematics
Are these two graphs isomorphic? Yes!
Graph B’s nodes have been moved to show the isomorphism.
1
0
3
4
Graph A
2
5
1
2
3
0
1
0
3
4
2
5
Graph B
4
5
Graph B with node 4 moved to the upper right, 2 moved to the upper center and 0 moved
to the lower center position. Now Graphs A and B look the same except for labeling.
4/13/2015
15
MATH 224 – Discrete Mathematics
Graph Properties - Degree
The degree of a node in an undirected graph is the number of edges incident to that
node.
0
1
2
3
4
5
What is the degree of node4? Of node 0? Of node 2?
4/13/2015
16
MATH 224 – Discrete Mathematics
Graph Properties – Complete Graphs
How is the degree of a graph related to the number of edges?
The minimum number of edges in a graph is 0. A graph with the maximum number
of edges is called a complete graph. The graph below is a complete undirected
graph with four nodes.
What is the degree of each node in an N node complete Graph?
What is the sum of the degrees in an N node complete Graph?
How many edges are there in an N node complete Graph?
4/13/2015
17
MATH 224 – Discrete Mathematics
Graph Properties – Bipartite Graphs
Bipartite graphs are graphs whose nodes can be divided into two disjoint sets, called
partitions, in which there are no edges between nodes within a partition.
A complete bipartite graph has all possible edges. So for example, if one partition
has K nodes and the other has M nodes, nodes in the K node partition will have
degree M and the nodes in the M node partition will have degree K.
How many edges will there be in a complete bipartite graph with partitions of
size K and M?
4/13/2015
18
MATH 224 – Discrete Mathematics
Graph Properties – Bipartite Graphs
Bipartite graphs have a number of interesting properties. For example, any cycle in a
bipartite graph must have an even number of edges. Note the cycle in red below
(0,4,3,5,0).
4/13/2015
0
1
4
5
2
3
19
MATH 224 – Discrete Mathematics
Graph Properties – Hypercube
Hypercubes have interesting properties that make them useful for some forms of
parallel computing. The text illustrates hypercubes for n = 1, 2, 3 on Page 549. The
figure below illustrates a 4-cube.
How many nodes are in an n-cube for n > 0? How many edges?
4/13/2015
20
MATH 224 – Discrete Mathematics
Graph Properties – Planar Graphs
Planar graphs are graphs that may be drawn on a 2-dimensional plane without having
any of the edges intersect. Below are several planar graphs that are also complete
graphs.
Try to draw a complete 5 node graph without having any edges intersect.
Can you do that?
4/13/2015
21
MATH 224 – Discrete Mathematics
Graph Properties – Planar Graphs
A complete 5 node graph is not planar. Planar graphs are important in circuit board
design. With a single layer circuit board the graph needs representing the circuit
needs to be planar so that traces do not intersect. In order to build more complicated
circuits multiple layer boards are often needed. In order to connect the edge in red to
node a it will have to cross one or more of the other edges unless it is allowed to go
outside of the plane.
a
4/13/2015
22
Download