Introduction to Graphs
Table of Contents
1 Reading
Read Bailey sections 16.1 and 16.2.
2 Video
Here is a video lecture for today's lesson.
The video contains sections on
- Defining a graph (0:18)
- Examples of graphs (2:02)
- Is a graph an ADT? (3:08)
- Undirected graph (4:20)
- Directed graph (8:23)
- Other notation (11:21)
- Graph examples (17:43)
- Trees as graphs (28:24)
The Panopto viewer has table of contents entries for these sections: https://carleton.hosted.panopto.com/Panopto/Pages/Viewer.aspx?id=71129df5-d1d0-41ac-a480-acd701859877
3 Defining a Graph
- A graph is a formalism for representing relationships among items
- Very general definition because very general concept
- A graph is a pair: \(G = (V,E)\)
- A set of vertices, also known as nodes: \(V = \{v_1 ,v_2 , \dots, v_n\}\)
- A set of edges \(E = \{e_1 , e_2 , \dots, e_m\}\)
- Each edge \(e_i\) is a pair of vertices \((v_j ,v_k)\)
- An edge connects the vertices
3.1 Examples
For each, what are the vertices and what are the edges?
- Web pages with links
- pages are vertices, embedded links the edges
- Methods in a program that call each other
- methods are vertices, calls are edges
- Road maps (e.g., Google maps)
- locations are vertices, streets/highways are edges
- Airline routes
- locations are vertices, flights are edges
- Family trees
- people are vertices, relationships are edges
- Course pre-requisites
- courses are vertices, pre-reqs are edges
3.2 Is Graph and ADT?
- Can think of graphs as an ADT with operations like
isEdge(v1, v2)
to check if there's an edge between two vertexes - But it is unclear what the standard operations are
- Instead we tend to develop algorithms over graphs and then use data structures that are efficient for those algorithms
- Many important problems can be solved by:
- Formulating them in terms of graphs
- Applying a standard graph algorithm
- To make the formulation easy and standard, we have a lot of standard terminology about graphs
4 Terminology
4.1 Undirected Graphs
- In undirected graphs, edges have no specific direction
- Edges are always two-way
- Only one of these edges needs to be in the set \(E\) (the other is implicit)
- Degree of a vertex: number of edges containing that vertex
- Put another way: the number of adjacent vertices
- A path is a sequence of vertices connected by edges, with no repeated edges
- Two vertices are connected if there is a path between them
- A graph is connected when there is a path between every two vertices
- A cycle is a path (with at least 1 edge) who first and last vertices are the same
- i.e., a loop
- A graph can by cyclic (is has cycles) or acyclic (it doesn't have cycles)
4.2 Directed Graphs
- In directed graphs (sometimes called digraphs), edges have a direction
- Thus, just because the edge \((u,v)\) is in the graph doesn't mean that \((v,u)\) is in the graph.
- Call \(u\) the source and \(v\) the destination
- In-degree of a vertex: number of in-bound edges (i.e., edges where the vertex is the destination)
- Out-degree of a vertex: number of out-bound edges (i.e., edges where the vertex is the source)
- Directed graphs can have paths and cycles as well
- Vertex \(w\) is reachable from vertex \(v\) if there is a directed path from \(v\) to \(w\)
4.3 Other Notation
- A node can have a degree / in-degree / out-degree of zero
- A graph does not have to be connected
- Even if every node has non-zero degree
- For a graph \(G = (V,E)\):
- \(|V|\) is the number of vertices
- \(|E|\) is the number of edges
- Minimum edges? \(0\)
- Maximum for undirected? \(\frac{|V||V+1|}{2}\), which is \(O(|V|^2)\)
- Maximum for directed? \(|V|^2 - |V|\), which is \(O(|V|^2)\)
- (unless vertices can have self-edges, in which case it is \(|V|^2\))
- If \((u,v)\) is in \(E\)
- Then \(v\) is a neighbor of \(u\), i.e., \(v\) is adjacent to \(u\)
- Order matters for directed edges
- \(u\) is not adjacent to \(v\) unless \((v,u)\) is in \(E\)
5 Weighted Graphs
- In a weighted graph, each edge has a weight a.k.a. cost
- Typically numeric (most examples use ints)
- Separate from whether graph is directed
- Some graphs allow negative weights; many do not
6 Examples Again
Which would use directed edges?
- Web pages with links
- Methods in a program that call each other
- Road maps (e.g., Google maps)
- one-way streets
- Airline routes
- possible but unlikely, at least in passenger travel (airlines generally want their planes to fly back from wherever they go)
- Course pre-requisites
Which would have self-edges?
- Web pages with links
- link to another part of the same page
- Methods in a program that call each other
- recursion!
Which would be connected?
- Road maps (e.g., Google maps)
- Family trees
Which could have 0-degree nodes?
- Web pages with links
- Facebook friends
- Methods in a program that call each other
- Road maps (e.g., Google maps)
- Airline routes
- Course pre-requisites
Which would have weighted edges?
- Road maps (e.g., Google maps)
- distance, travel time
- Airline routes
- distance, travel time, airfare
7 Are Trees Graphs?
- When talking about graphs, we say a tree is a graph that is:
- Undirected
- Acyclic
- Connected
- So all trees are graphs, but not all graphs are trees
An example:
- How does this relate to the trees we know and love?
- We are more accustomed to rooted trees where:
- We identify a unique root
- We think of edges as directed: parent to children
- Every rooted tree is a directed acyclic graph (DAG)
- Given a tree, picking a root gives a unique rooted tree
- The tree is just drawn differently and with directed edges
8 Practice Problems1
- What is the difference between an undirected graph and a directed graph?
- What is the difference between a graph and a tree?
- For each of the following, consider how you might represent it as a graph. What would be the vertices and what would be the edges?
- infectious disease
- a criminal conspiracy
- a city's water system
- people texting each other
- international diplomacy
- the floor plan of a house
- Which of these graphs is best modeled as a directed graph?
- Facebook: vertex = person; edge = friendship.
- Web: vertex = web page; edge = URL link.
- Internet: vertex = router; edge = fiber optic cable.
- Molecule: vertex = atom; edge = chemical bond.
- Review the new terms from this lesson: graph, edge, vertex, directed, undirected, degree, in-degree, out-degree, path, connected, cycle, cyclic, acyclic, adjacent vertex, weighted graph
9 Learning Block
Footnotes:
1
Solutions:
- A tree does not have cycles; a graph can. All trees are graphs, but not all graphs are trees.
- An directed graph has oriented edges—edges that establish a potentially one-way relation between vertices. The edges of an undirected graph establish a symmetric relationship.
- vertexes = people, edges = disease transmission
- vertexes = suspected conspirators or evidence, edges = possible connections (think red string board)
- vertexes = water sources or points of consumption, edges = pipes
- vertexes = cell phones, edges = texts
- vertexes = nations, edges = alliances
- vertexes = rooms, edges = doors, hallways, stairs
- The web would be best modeled as a directed graph because one web page linking to another doesn't mean the linked page links back