# Difference between revisions of "Graph Theory"

(Created page with "== Overview == Many problems are naturally formulated in terms of points and connections between them. For example, a computer network has PCs connected by cables, an airlin...") |
(→ACSL Videos) |
||

(47 intermediate revisions by 2 users not shown) | |||

Line 1: | Line 1: | ||

+ | Many problems are naturally formulated in terms of points and connections between them. For example, a computer network has PCs connected by cables, an airline map has cities connected by routes, and a school has rooms connected by hallways. A graph is a mathematical object which models such situations. | ||

+ | |||

== Overview == | == Overview == | ||

− | + | A ''graph'' is a collection of vertices and edges. An ''edge'' is a connection between two ''vertices'' (sometimes referred to as ''nodes''). One can draw a graph by marking points for the vertices and drawing lines connecting them for the edges, but the graph is defined independently of the visual representation. For example, the following two drawings represent the same graph: | |

− | + | ::[[File:Graph fig1a.svg|150px]] <span style="display:inline-block; width: 5em;"></span>[[File:Graph fig1b.svg|150px]] | |

+ | The precise way to represent this graph is to identify its set of vertices {A, B, C, D, E, F, G}, and its set of edges between these vertices {AB, AD, BD, CF, FG, GH, GE, HE}. | ||

+ | To be precise, the graph above is an ''undirected graph''; the edge from x to y is the same as from y to x. | ||

== Terminology == | == Terminology == | ||

− | + | A ''path'' from vertex x to y in a graph is a list of vertices, in which successive vertices are connected by edges in the graph. For example, FGHE is path from F to E in the graph above. A ''simple path'' is a path with no vertex repeated. For example, FGHEG is not a simple path. | |

− | A path from vertex | + | A graph is ''connected'' if there is a path from every vertex to every other vertex in the graph. Intuitively, if the vertices were physical objects and the edges were strings connecting them, a connected graph would stay in one piece if picked up by any vertex. A graph which is not connected is made up of ''connected components''. For example, the graph above has two connected components: {A, B, D} and {C, E, F, G, H}. |

− | A | + | A ''cycle'' is a path, which is simple except that the first and last vertex are the same (a path from a point back to itself). For example, the path HEGH is a cycle in our example. Vertices must be listed in the order that they are traveled to make the path; any of the vertices may be listed first. Thus, HEGH and EHGE are different ways to identify the same cycle. For clarity, we list the start / end vertex twice: once at the start of the cycle and once at the end. |

− | + | We’ll denote the number of vertices in a given graph by V and the number of edges by E. Note that E can range anywhere from V to V<sup>2</sup> (or V(V-1)/2 in an undirected graph). Graphs with all edges present are called ''complete graphs''; graphs with relatively few edges present (say less than V log(V)) are called ''sparse''; graphs with relatively few edges missing are called ''dense.'' | |

− | + | ||

− | We’ll denote the number of vertices in a given graph by V | + | |

== Trees == | == Trees == | ||

− | A graph with no cycles is called a tree. There is only one path between any two nodes in a tree. A tree on N vertices contains exactly N-1 edges. A spanning tree of a graph is a subgraph that contains all the vertices and forms a tree. A group of disconnected trees is called a forest. | + | A graph with no cycles is called a ''tree''. There is only one path between any two nodes in a tree. A tree on N vertices contains exactly N-1 edges. A ''spanning tree'' of a graph is a subgraph that contains all the vertices and forms a tree. Minimal spanning trees can be found for weighted graphs (i.e. each edge has a cost or distance associated with it) in order to minimize the cost or distance across an entire network. A group of disconnected trees is called a ''forest.'' |

− | Directed | + | == Directed Graphs == |

+ | ''Directed graphs'' are graphs which have a direction associated with each edge. An edge xy in a directed graph can be used in a path that goes from x to y but not necessarily from y to x. For example, a directed graph similar to our example graph is drawn below: | ||

+ | [[File:Graph fig1a directed.svg|150px]] | ||

− | There is | + | This graph is defined as the set of vertices V = {A,B,C,D,E,F,G,H} and the set of edges {AB,AD,DA,DB,EG,GE,HG,HE,GF,CF,FC}. There is one directed path from G to C (namely, GFC); however, there are no directed paths from C to G. Note that a few of the edges have arrows on both ends, such as the edge between A and D. These dual arrows indicate that there is an edge in each direction, which essentially makes an undirected edge. An ''undirected graph'' can be thought of as a directed graph with all edges occurring in pairs in this way. A directed graph with no cycles is called a ''dag'' (directed acyclic graph). |

== Adjacency Matrices == | == Adjacency Matrices == | ||

− | It is frequently convenient to represent a graph by a matrix, as shown in the second sample problem below. If we consider vertex A as 1, B as 2, etc., then a “one” in M at row i and column j indicates that there is | + | It is frequently convenient to represent a graph by a matrix, as shown in the second sample problem below. If we consider vertex A as 1, B as 2, etc., then a “one” in M at row ''i'' and column ''j'' indicates that there is an edge from vertex ''i'' to ''j''., whereas a "zero" indicates there is not an edge. If we raise M to the ''pth'' power, the resulting matrix indicates which paths of length ''p'' exist in the graph. The value in $M^p(i,j)$ is the number of paths from vertex ''i'' to vertex ''j''. |

− | == | + | == Sample Problems == |

− | + | === Problem 1 === | |

+ | Find the number of different cycles contained in the directed graph with vertices {A, B, C, D, E} and edges {AB, BA, BC, CD, DC, DB, DE}. | ||

+ | '''Solution:''' | ||

+ | The graph is as follows: | ||

− | + | ::[[File:graph sample1.svg|196px]] | |

− | == Sample | + | By inspection, the cycles are: ABA, BCDB, and CDC. Thus, there are 3 cycles in the graph. |

+ | <!-- | ||

+ | === Sample Problem 2 === | ||

− | + | Given the directed graph below, how many more paths of length 3 would exist if edges AE and EA were added? | |

− | + | [[File:graph_s1.png]] | |

'''Solution:''' | '''Solution:''' | ||

− | |||

+ | By using adjacency matrices, the current one is | ||

+ | [[File:matrix1_s2.png]] | ||

− | + | After adding edges AE and EA, the matrices are: | |

− | + | [[File:matrix2_s2.png]] | |

− | + | There are 6 more paths of length 2 since 16 – 10 = 6. | |

− | + | --> | |

− | + | === Problem 2=== | |

− | + | ||

− | + | ||

− | + | ||

− | + | ||

− | + | In the following directed graph, find the total number of different paths from vertex A to vertex C of length 2 or 4. | |

− | + | ::[[File:graph sample3.svg|128px]] | |

− | : | + | |

'''Solution:''' | '''Solution:''' | ||

− | |||

− | |||

− | + | Let matrix $M$ represent the graph. Recall that the number of paths from vertex $i$ to vertex $j$ of length $p$ equals $M^p[i,j]$. The values of $M$, $M^2$ and $M^4$ are: | |

− | + | [[File:matrix_s3.png]] | |

− | + | ||

− | + | There is 1 path of length 2 from A to C (cell [1,3] in $M^2$) | |

− | + | and 3 paths of length 4 (cell [1,3] in $M^4$). | |

− | + | ||

− | + | ||

− | + | === Problem 3 === | |

− | + | Given the adjacency matrix, draw the directed graph. | |

− | + | ||

− | + | ||

− | + | [[File:matrix_s4.png]] | |

− | : A | + | |

+ | '''Solution:''' | ||

+ | |||

+ | There must be exactly 4 vertices: V = {A, B, C, D}. There must be be exactly 7 edges: E = {AB, AD, BA, BD, CA, DB, DC}. Here are two valid drawings of the graph: | ||

+ | |||

+ | : [[File:Graph sample4soln-a.svg|128px]] <span style="display:inline-block; width: 5em;"></span>[[File:Graph sample4soln-d.svg|128px]] | ||

== Video Resources == | == Video Resources == | ||

+ | |||

+ | ===ACSL Videos=== | ||

The following YouTube videos show ACSL students and advisors working out some ACSL problems that have appeared in previous contests. Some of the videos contain ads; ACSL is not responsible for the ads and does not receive compensation in any form for those ads. | The following YouTube videos show ACSL students and advisors working out some ACSL problems that have appeared in previous contests. Some of the videos contain ads; ACSL is not responsible for the ads and does not receive compensation in any form for those ads. | ||

Line 99: | Line 104: | ||

{| | {| | ||

|- | |- | ||

− | | <youtube width="300" height="180">https://youtu.be/ | + | | <youtube width="300" height="180">https://youtu.be/H96Lo2q_FSk</youtube> |

− | | [https://youtu.be/ | + | | [https://youtu.be/H96Lo2q_FSk ''ACSL - Graph Theory Worksheet 1'' ('''misterminich''')] |

− | + | ||

− | + | ||

+ | Shows the solution to the ACSL problem: '''Draw the directed graph with vertices A, B, C, D, E and the directed edges AB, BC, AD, BC, DC, ED, and EA.'' | ||

|- | |- | ||

− | | <youtube width="300" height="180">https://youtu.be/ | + | | <youtube width="300" height="180">https://youtu.be/y1fdIb_oNG0</youtube> |

− | | [https://youtu.be/ | + | | [https://youtu.be/y1fdIb_oNG0 ''ACSL Graph Theory Worksheet 3'' ('''misterminich''')] |

− | + | ||

− | + | ||

− | + | ||

+ | Shows the solution to an ACSL problem asking to find how many paths of a specific length there are in a specific directed graph. | ||

|- | |- | ||

− | | <youtube width="300" height="180">https://youtu.be/ | + | | <youtube width="300" height="180">https://youtu.be/vtTCRMGB0K8</youtube> |

− | | [https://youtu.be/ | + | | [https://youtu.be/vtTCRMGB0K8 ''Graph Theory ACSL Example Problem'' ('''Tangerine Code''')] |

− | Shows the solution to | + | Shows the solution to an ACSL problem asking to find how many different cycles there are from a specific vertex in a specific directed graph. |

+ | |} | ||

+ | {| | ||

|- | |- | ||

− | | <youtube width="300" height="180">https://youtu.be/ | + | | <youtube width="300" height="180">https://youtu.be/ivhOH6crQ1w</youtube> |

− | | [https://youtu.be/ | + | | [https://youtu.be/ivhOH6crQ1w ''ACSL Test Prep - Graph Theory'' ('''Mrs. Goopta''')] |

− | + | ||

− | + | ||

− | + | ||

− | + | ||

− | + | ||

− | + | ||

− | + | ||

− | + | ||

+ | A talked-over presentation discussing graph theory as needed for the American Computer Science League and its tests. | ||

|} | |} | ||

+ | ===Other Videos=== | ||

+ | There are dozens of YouTube videos that provide an introduction to graph theory. The following are a couple that we found particularly well done. Be aware that the videos contain ads; ACSL is not responsible for the ads and does not receive compensation in any form for those ads. | ||

− | |||

{| | {| | ||

|- | |- | ||

− | | <youtube width="300" height="180"> | + | | <youtube width="300" height="180">https://youtu.be/44sQJK4BycY</youtube> |

− | | [ | + | | [https://youtu.be/44sQJK4BycY ''CS 106B Lecture: Graphs: basic concepts''] |

− | + | This lecture from Stanford University is s wonderful introduction to graph theory. The lecture starts out with many examples of real world problems that are modeled by graphs, and then moves on to review basic terminology relating to graph theory. | |

+ | |- | ||

+ | | <youtube width="300" height="180"> https://youtu.be/gXgEDyodOJU</youtube> | ||

+ | | [https://youtu.be/gXgEDyodOJU ''Data structures: Introduction to Graphs'' '''(mycodeschool)'''] | ||

+ | |||

+ | A nice introduction to the fundamental concepts of graphs. | ||

+ | |||

|} | |} | ||

− |

## Latest revision as of 23:46, 9 June 2020

Many problems are naturally formulated in terms of points and connections between them. For example, a computer network has PCs connected by cables, an airline map has cities connected by routes, and a school has rooms connected by hallways. A graph is a mathematical object which models such situations.

## Contents

## Overview

A *graph* is a collection of vertices and edges. An *edge* is a connection between two *vertices* (sometimes referred to as *nodes*). One can draw a graph by marking points for the vertices and drawing lines connecting them for the edges, but the graph is defined independently of the visual representation. For example, the following two drawings represent the same graph:

The precise way to represent this graph is to identify its set of vertices {A, B, C, D, E, F, G}, and its set of edges between these vertices {AB, AD, BD, CF, FG, GH, GE, HE}.

To be precise, the graph above is an *undirected graph*; the edge from x to y is the same as from y to x.

## Terminology

A *path* from vertex x to y in a graph is a list of vertices, in which successive vertices are connected by edges in the graph. For example, FGHE is path from F to E in the graph above. A *simple path* is a path with no vertex repeated. For example, FGHEG is not a simple path.

A graph is *connected* if there is a path from every vertex to every other vertex in the graph. Intuitively, if the vertices were physical objects and the edges were strings connecting them, a connected graph would stay in one piece if picked up by any vertex. A graph which is not connected is made up of *connected components*. For example, the graph above has two connected components: {A, B, D} and {C, E, F, G, H}.

A *cycle* is a path, which is simple except that the first and last vertex are the same (a path from a point back to itself). For example, the path HEGH is a cycle in our example. Vertices must be listed in the order that they are traveled to make the path; any of the vertices may be listed first. Thus, HEGH and EHGE are different ways to identify the same cycle. For clarity, we list the start / end vertex twice: once at the start of the cycle and once at the end.

We’ll denote the number of vertices in a given graph by V and the number of edges by E. Note that E can range anywhere from V to V^{2} (or V(V-1)/2 in an undirected graph). Graphs with all edges present are called *complete graphs*; graphs with relatively few edges present (say less than V log(V)) are called *sparse*; graphs with relatively few edges missing are called *dense.*

## Trees

A graph with no cycles is called a *tree*. There is only one path between any two nodes in a tree. A tree on N vertices contains exactly N-1 edges. A *spanning tree* of a graph is a subgraph that contains all the vertices and forms a tree. Minimal spanning trees can be found for weighted graphs (i.e. each edge has a cost or distance associated with it) in order to minimize the cost or distance across an entire network. A group of disconnected trees is called a *forest.*

## Directed Graphs

*Directed graphs* are graphs which have a direction associated with each edge. An edge xy in a directed graph can be used in a path that goes from x to y but not necessarily from y to x. For example, a directed graph similar to our example graph is drawn below:

This graph is defined as the set of vertices V = {A,B,C,D,E,F,G,H} and the set of edges {AB,AD,DA,DB,EG,GE,HG,HE,GF,CF,FC}. There is one directed path from G to C (namely, GFC); however, there are no directed paths from C to G. Note that a few of the edges have arrows on both ends, such as the edge between A and D. These dual arrows indicate that there is an edge in each direction, which essentially makes an undirected edge. An *undirected graph* can be thought of as a directed graph with all edges occurring in pairs in this way. A directed graph with no cycles is called a *dag* (directed acyclic graph).

## Adjacency Matrices

It is frequently convenient to represent a graph by a matrix, as shown in the second sample problem below. If we consider vertex A as 1, B as 2, etc., then a “one” in M at row *i* and column *j* indicates that there is an edge from vertex *i* to *j*., whereas a "zero" indicates there is not an edge. If we raise M to the *pth* power, the resulting matrix indicates which paths of length *p* exist in the graph. The value in $M^p(i,j)$ is the number of paths from vertex *i* to vertex *j*.

## Sample Problems

### Problem 1

Find the number of different cycles contained in the directed graph with vertices {A, B, C, D, E} and edges {AB, BA, BC, CD, DC, DB, DE}.

**Solution:**
The graph is as follows:

By inspection, the cycles are: ABA, BCDB, and CDC. Thus, there are 3 cycles in the graph.

### Problem 2

In the following directed graph, find the total number of different paths from vertex A to vertex C of length 2 or 4.

**Solution:**

Let matrix $M$ represent the graph. Recall that the number of paths from vertex $i$ to vertex $j$ of length $p$ equals $M^p[i,j]$. The values of $M$, $M^2$ and $M^4$ are:

There is 1 path of length 2 from A to C (cell [1,3] in $M^2$) and 3 paths of length 4 (cell [1,3] in $M^4$).

### Problem 3

Given the adjacency matrix, draw the directed graph.

**Solution:**

There must be exactly 4 vertices: V = {A, B, C, D}. There must be be exactly 7 edges: E = {AB, AD, BA, BD, CA, DB, DC}. Here are two valid drawings of the graph:

## Video Resources

### ACSL Videos

The following YouTube videos show ACSL students and advisors working out some ACSL problems that have appeared in previous contests. Some of the videos contain ads; ACSL is not responsible for the ads and does not receive compensation in any form for those ads.

ACSL - Graph Theory Worksheet 1 (misterminich)
Shows the solution to the ACSL problem: ' | |

ACSL Graph Theory Worksheet 3 (misterminich)
Shows the solution to an ACSL problem asking to find how many paths of a specific length there are in a specific directed graph. | |

Graph Theory ACSL Example Problem (Tangerine Code)
Shows the solution to an ACSL problem asking to find how many different cycles there are from a specific vertex in a specific directed graph. |

ACSL Test Prep - Graph Theory (Mrs. Goopta)
A talked-over presentation discussing graph theory as needed for the American Computer Science League and its tests. |

### Other Videos

There are dozens of YouTube videos that provide an introduction to graph theory. The following are a couple that we found particularly well done. Be aware that the videos contain ads; ACSL is not responsible for the ads and does not receive compensation in any form for those ads.

CS 106B Lecture: Graphs: basic concepts
This lecture from Stanford University is s wonderful introduction to graph theory. The lecture starts out with many examples of real world problems that are modeled by graphs, and then moves on to review basic terminology relating to graph theory. | |

Data structures: Introduction to Graphs (mycodeschool)
A nice introduction to the fundamental concepts of graphs. |