Suppose that \(\mathcal{L} = (A_1, A_2, \ldots, A_i)\) is the adjacency list for \(K_i\), the complete graph on \(i\) vertices.
For \(i+1\) vertices, simply add vertex \(i+1\) to each linked list, and then add an additional linked list to \(\mathcal{L}\) that contains all vertices except for \(i+1\).
\[ A_{i+1} = 1\rightarrow 2 \rightarrow 3 \rightarrow \cdots \rightarrow i \]
Guanacaste (1),Alajuela(2), Heredia(3), Limón (4), Cartago (5), San José (6), Puntarenas (7)
igraph
plot optionslibrary(igraph) #load the igraph library
costaRica_list <- list(c(2,7),
c(1,7,3,6),
c(2,4,6),
c(5,3,6,7),
c(4,6),
c(7,5,3,4,2),
c(6,4,2,1))
M <- graph_from_adj_list(costaRica_list, mode="all")
oldPar <- par(mar=c(0.1,0.1,0.1,0.1)) # reduce the margins
set.seed(1234) # fix the randomization of the following plot command
plot(M, vertex.color= c("blue","yellow","red","yellow","red","blue","red"))
par(oldPar) # restore the default margins
For a list of options for plotting using igraph
:
library(igraph) #load the igraph library
costaRica_list <- list(c(2,7),
c(1,7,3,6),
c(2,4,6),
c(5,3,6,7),
c(4,6),
c(7,5,3,4,2),
c(6,4,2,1))
M <- graph_from_adj_list(costaRica_list, mode="all")
oldPar <- par(mar=c(0.1,0.1,0.1,0.1)) # reduce the margins
set.seed(8723) # fix the randomization of the following plot command
plot(M, vertex.color= c("blue","yellow","red","yellow","red","blue","red"))
par(oldPar) # restore the default margins
library(igraph) #load the igraph library
costaRica_list <- list(c(2,7),
c(1,7,3,6),
c(2,4,6),
c(5,3,6,7),
c(4,6),
c(7,5,3,4,2),
c(6,4,2,1))
M <- graph_from_adj_list(costaRica_list, mode="all")
oldPar <- par(mar=c(0.1,0.1,0.1,0.1)) # reduce the margins
set.seed(31205) # fix the randomization of the following plot command
plot(M, vertex.color= c("orange","yellow","red","yellow","red","orange","red"),
vertex.label=c("Guanacaste","Alajuela","Heredia","Limón","Cartago","San José","Puntarenas"),
vertex.size=55)
par(oldPar) # restore the default margins
Any planar graph can be four colored.
Our default representation will be as an adjacency list.
A[1..V]
is an array of lists.
A[i]
is a list of neighbors of vertex i
A[i]
is a list of out-neighbors of vertex i
Sometimes it is convenient to represent a graph as a \(V \times V\) matrix:
\[ \begin{bmatrix} a_{1,1} & a_{1,2} & \cdots & a_{1,V} \\ a_{2,1} & a_{2,2} & \cdots & a_{2,V} \\ \vdots & \vdots && \vdots \\ a_{V,1} & a_{V,2} & \cdots & a_{V,V} \end{bmatrix} \]
Push(x)
inserts x
at the top of the stackPop
removes the element from the top of the stack and returns it.Enqueue(x)
inserts x
at the back of the queueDequeue
removes the element from the front of the queue and returns it.Table #1 | Table #2 | Table #3 | Table #4 | Table #5 | Table #6 | Table #7 |
---|---|---|---|---|---|---|
Nathan | Kevin | Logan | Ethan | James | Jack | Drake |
Levi | Isaac | Talia | John | Blake | Josiah | Grace |
Kristen | Trevor | Bri | Jordan | Claire | Graham | Andrew |
Step through BFS on the Jamboard for the given graph and adjacency list. Keep track of the queue and make sure you record the order in which the nodes were visited.
How could you modify the DFS and BFS algorithms to deal with directed graphs?
Modified DepthFirstSearch
:
(p,v)
in the stack, where p
is the previously visited node.parent
attribute for each vertex.Similarly, we can modify BreadthFirstSearch
to get the breadth-first spanning tree.
BreadthFirstSearch(s): BreadthFirstSearchPlus(s):
Enqueue(s) Enqueue((∅,s))
while the queue is not empty while the queue is not empty
v <- Dequeue (p,v) <- Dequeue
if v is unmarked if v is unmarked
mark v mark v
for each edge vw v.parent <- p
Enqueue(w) for each edge vw
Enqueue((v,w))
Bonus: The breadth-first spanning tree contains the shortest path from s
to every node.
WhateverFirstSearch(s):
put s into the bag
while the bag is not empty
take v from the bag
if v is unmarked
mark v
for each edge vw
put w into the bag
s
: shortest paths from s
(Dijkstra)s
: widest paths (Maximum Flow)WhateverFirstSearch(s):
put s into the bag
while the bag is not empty
take v from the bag
if v is unmarked
mark v
for each edge vw
put w into the bag
Time: \(O(V + ET)\) where \(T\) is the time to insert/delete to/from bag