graph theory 2 - cs.indstate.educs.indstate.edu/sternfl/graph2.pdf · directions: make the le:...

3
Graph Theory 2 Graphs on computers. Below is a graph with 4 vertices labeled v 0 ,v 1 ,v 2 ,v 3 and 5 edges labeled e 0 ,e 1 ,e 2 ,e 3 ,e 4 We can represent the graph with the incidence matrix. The entry 1 in a row and column means that the vertex (row) is incident (on) the edge (column). Entry 0 means the edge is not incident with the vertex. Incidence = e 0 e 1 e 2 e 3 e 4 v 0 1 0 0 1 0 v 1 0 0 1 1 1 v 2 0 1 0 0 1 v 3 1 1 1 0 0 Note the incidence matrix is a 2–dimensional python list. The term 2–dimensional means Incidence is a list of lists. Python incidence array: Incidence = [ [1,0,0,1,0], [0,0,1,1,1], [0,1,0,0,1], [1,1,1,0,0] ] Another way of storing graphs on computers is the Adjacency matrix. An entry of 1 in a row and column means that the vertex of the row and the vertex of the column are connected by an edge. An entry of 0 means that the vertex of the row and column are not connected by an edge. Adjacency = v 0 v 1 v 2 v 3 v 0 0 1 0 1 v 1 1 0 1 1 v 2 0 1 0 1 v 3 1 1 1 0 In Python adjacency array: Adjacency = [ [0,1,0,1], [1,0,1,1], [0,1,0,1], [1,1,1,0] ]

Upload: others

Post on 18-Aug-2020

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Graph Theory 2 - cs.indstate.educs.indstate.edu/sternfl/graph2.pdf · DIRECTIONS: Make the le: graph2.txt. Put the answers to the following 5 questions in the le. Leave the le in

Graph Theory 2

Graphs on computers.

Below is a graph with 4 vertices labeled v0, v1, v2, v3 and 5 edges labeled e0, e1, e2, e3, e4

We can represent the graph with the incidence matrix. The entry 1 in a row and column means thatthe vertex (row) is incident (on) the edge (column). Entry 0 means the edge is not incident with the vertex.

Incidence =

e0 e1 e2 e3 e4

v0 1 0 0 1 0v1 0 0 1 1 1v2 0 1 0 0 1v3 1 1 1 0 0

Note the incidence matrix is a 2–dimensional python list. The term 2–dimensional means Incidence is a listof lists. Python incidence array:

Incidence = [

[1,0,0,1,0],

[0,0,1,1,1],

[0,1,0,0,1],

[1,1,1,0,0]

]

Another way of storing graphs on computers is the Adjacency matrix. An entry of 1 in a row and columnmeans that the vertex of the row and the vertex of the column are connected by an edge. An entry of 0means that the vertex of the row and column are not connected by an edge.

Adjacency =

v0 v1 v2 v3

v0 0 1 0 1v1 1 0 1 1v2 0 1 0 1v3 1 1 1 0

In Python adjacency array:

Adjacency = [

[0,1,0,1],

[1,0,1,1],

[0,1,0,1],

[1,1,1,0]

]

Robert Sternfeld
Robert Sternfeld
Robert Sternfeld
Robert Sternfeld
Robert Sternfeld
Robert Sternfeld
Robert Sternfeld
Robert Sternfeld
Robert Sternfeld
Robert Sternfeld
v
Robert Sternfeld
v
Robert Sternfeld
v
Robert Sternfeld
v
Robert Sternfeld
0
Robert Sternfeld
3
Robert Sternfeld
2
Robert Sternfeld
1
Robert Sternfeld
e
Robert Sternfeld
e
Robert Sternfeld
e
Robert Sternfeld
0
Robert Sternfeld
1
Robert Sternfeld
4
Robert Sternfeld
3
Robert Sternfeld
2
Robert Sternfeld
e
Robert Sternfeld
e
Page 2: Graph Theory 2 - cs.indstate.educs.indstate.edu/sternfl/graph2.pdf · DIRECTIONS: Make the le: graph2.txt. Put the answers to the following 5 questions in the le. Leave the le in

Definition: A path from a vertex v0 to a vertex vn is a sequence of vertices and edges

[v0, e1, v1, e2, v2, ..., en, vn]

so that:1. Edge ei connects vi−1 and vi.2. There are no repeated edges.3. There are no repeated vertices except maybe when v0 == vn.

The length of a path is the number of edges in it. A path is a cycle if v0 == vn.

In a directed graph, a directed path follows the previous definition with the addition that edge ei is a directededge from vertex vi−1 to vi.

A undirected graph is said to be connected if every pair of vertices is connected by a path. A directed graph(digraph) is said to be strongly connected if for every pair of vertices (v0, vn) there is directed path from v0to vn. In the graph below there is an directed path for the pair (v1, v3), but there is NO directed path forthe pair (v3, v1).

Weighted edges. Sometimes we associate a weight (or cost) with each edge. See below. You can think ofthe weight as being the length of the edge or the cost of traveling over the edge.

There are two paths from v1 to v2. See below. The total cost of the first path is 3 and the total cost of thesecond path is 1+1=2.

[v1, v2][v1, v3, v2]

Note: we can leave off the edges that we have been putting between the vertices. Edges are essential onlyif the graph has multiple edges:

Robert Sternfeld
Robert Sternfeld
Robert Sternfeld
Robert Sternfeld
e
Robert Sternfeld
v
Robert Sternfeld
v
Robert Sternfeld
i
Robert Sternfeld
i-1
Robert Sternfeld
i
Robert Sternfeld
Robert Sternfeld
Robert Sternfeld
Robert Sternfeld
Robert Sternfeld
Robert Sternfeld
v2
Robert Sternfeld
v1
Robert Sternfeld
v3
Robert Sternfeld
Robert Sternfeld
Robert Sternfeld
Robert Sternfeld
v2
Robert Sternfeld
v1
Robert Sternfeld
v3
Robert Sternfeld
Robert Sternfeld
Robert Sternfeld
Robert Sternfeld
3
Robert Sternfeld
1
Robert Sternfeld
1
Robert Sternfeld
Robert Sternfeld
Robert Sternfeld
Robert Sternfeld
Robert Sternfeld
Robert Sternfeld
Robert Sternfeld
e3
Robert Sternfeld
e2
Robert Sternfeld
e1
Robert Sternfeld
v3
Robert Sternfeld
v1
Robert Sternfeld
v2
Robert Sternfeld
Robert Sternfeld
[v1,e1,v2,e3,v3]�
Robert Sternfeld
[v1,e2,v2,e3,v3]
Page 3: Graph Theory 2 - cs.indstate.educs.indstate.edu/sternfl/graph2.pdf · DIRECTIONS: Make the le: graph2.txt. Put the answers to the following 5 questions in the le. Leave the le in

DIRECTIONS: Make the file: graph2.txt. Put the answers to the following 5 questions in the file. Leavethe file in your home directory.

1. Make the Python Incidence array for the graph above.

2. Make the Python Adjacency array for the graph above.

3. Consider the first graph on page1. Write the clockwise cycle with 3 edges that starts and ends with vertexv2.

4. Consider the first graph on page1. Write the clockwise cycle with 4 edges that starts and ends with vertexv2.

5. Consider the weighted graph displayed below. Write the path from vertex v1 to v3 that has minimal totalcost. Note: You only need to list the vertices.

Robert Sternfeld
Robert Sternfeld
Robert Sternfeld
Robert Sternfeld
Robert Sternfeld
Robert Sternfeld
Robert Sternfeld
Robert Sternfeld
v0
Robert Sternfeld
v1
Robert Sternfeld
v3
Robert Sternfeld
v2
Robert Sternfeld
Robert Sternfeld
Robert Sternfeld
Robert Sternfeld
v4
Robert Sternfeld
v6
Robert Sternfeld
v5
Robert Sternfeld
Robert Sternfeld
Robert Sternfeld
Robert Sternfeld
e0
Robert Sternfeld
e1
Robert Sternfeld
e2
Robert Sternfeld
e3
Robert Sternfeld
e5
Robert Sternfeld
e4
Robert Sternfeld
Robert Sternfeld
Robert Sternfeld
Robert Sternfeld
Robert Sternfeld
v1
Robert Sternfeld
v2
Robert Sternfeld
v3
Robert Sternfeld
v4
Robert Sternfeld
Robert Sternfeld
Robert Sternfeld
Robert Sternfeld
Robert Sternfeld
2
Robert Sternfeld
2
Robert Sternfeld
2
Robert Sternfeld
1