neighbors as the adjacency list of
s
in
G
8.
For each
t
∈
list
of
neighbors:
9.
If not visited(
t
):
10.
ExploreSystem(
t
,
G
)
GenerateInducedGraph(
G
0
,
dir
)
1.
G
:=
G
0
2.
For each vertex
v
∈
G
:
3.
if (isRed(
v
,
dir
) ==
false
):
4.
remove
v
from
G
by removing its adjacency list
5.
For each edge
(
u
,
v
)
∈
G
:
6.
if (isRed(
v
,
dir
) ==
false
):
7.
remove edge
(
u
,
v
)
from
G
8.
Return
G
HasInfiniteRedLoops(
s
0
)
1.
G
= GenerateGraph(
s
0
)
2.
For
i
:
=
1
,
2
,
3
,
4:
3.
G
i
:= GenerateInducedGraph(
G
,
dir
i
):
4.
if (hasCycle(
G
i
) ==
true
):
5.
Return
true
6.
Return
false
Proof of correctness.
The procedures GenerateGraph() and ExploreSystem() correctly generate an adja-
cency list of
G
since essentially they perform a DFS in G and in line 7 of procedure ExploreSystem() we
add in
G
the adjacency list of the vertex we visit and then we recursively explore this adjacency list. Also,
the procedure GenerateInducedGraph() generates correctly an induced graph
G
i
since it removes from
G
all
the vertices (states) that have green light in direction
d
i
and all of their incoming edges.
The only thing that remains to be proved (and also it implies the correctness of the procedure HasIn-
finiteLoops()) is that a car can wait infinitely in a direction
dir
i
if and only if there is a cycle in the induced
subgraph
G
i
.
Let’s prove first the direction: a car can wait infinitely in a direction
dir
i
⇒
there is a cycle in the induced
subgraph
G
i
. Well, if a car can wait infinitely in a direction
dir
i
then it means that there is a cycle in
G
where
all
of its vertices (or equivalently states) have red light in that direction
dir
i
. However, by construction of
the inducted graph
G
i
we don’t remove any vertices from the original graph
G
that have red light in that
direction
dir
i
neither we remove any edges of
G
that go to such vertices. Thus, the same cycle of
G
should
be also found in
G
i
.
The other direction: there is a cycle in the induced subgraph
G
i
⇒
a car can wait infinitely in a direction
dir
i
.
This follows from the fact that the vertices and the edges of the induced subgraph
G
i
can be also
CS 170, Fall 2014, Sol 4
12

