# Problem 1: The Class P (35 points)

- (25) Show that the class $P$ is closed under union, intersection, concatenation and complement.

## Answer:

Success

## Union

Let $L_{1}$ and $L_{2}$ be two languages in P. These languages are decidable and may be decided by algorithms $A_{1}$ and $A_{2}$. We can construct a new algorithm $A$ which decides the union of $L_{1}$ and $L_{2}$. M = “On input x:

- Run $A_{1}$ on input x.
- Run $A_{2}$ on input x.
- If either A1 or A2 accept x, then A accepts x.
The running time of A is the sum of the running times of A1 and A2. These algorithms would be polynomial-time. Thus, the union of $L_{1}$ and $L_{2}$ is in P.

## Intersection

Similarly to union, if we have two languages $L_{1}$ and $L_{2}$ in P we may construct a new algorithm A that decides the union of $L_{1}$ and $L_{2}$ with corresponding algorithms $A_{1}$ and $A_{2}$. M = “On input x:

- Run $A_{1}$ on input x.
- Run $A_{2}$ on input x.
- If either A1 or A2 accept x, then A accepts x.
Like before, the running time of A is the sum of the running times of A1 and A2. These algorithms would be polynomial-time. Thus, the intersection of $L_{1}$ and $L_{2}$ is in P.

## Concatenation:

If we have $L_{1}$ and $L_{2}$ we may construct a new language $L_{3}=L_{1}L_{2}$. $L_{3}$ consists of all strings which can be split into two parts with part one in $L_{1}$ and part two in $L_{2}$. We may construct an algorithm on $L_{1}$ & $L_{2}$ to determine if they are in $L_{3}$: M = “On string input x:

- Run $L_{1}$ on input x to check if the first slice of the string is in $L_{1}$.
- Run $L_{2}$ on input x to check if the second slice of the string is in $L_{2}$.
- If the above conditions are both met, we accept x, otherwise reject x.
Because the potential number of splits is linear, we may perform these checks in polynomial-time the overall runtime is polynomial. Thus, the concatenation of $L_{1}$ and $L_{2}$ is in P

## Complement

If we have language $L_{1}$ in P we may construct a new language $L_{2}$ which is the complement of $L_{1}$. We can construct an algorithm to decide whether string x is in L2: M = “On string input x:

- Run $L_{1}$ on input x to check if x is
NOTin L1.- If x is
NOTin $L_{1}$, accept, otherwise reject.This algorithm runs in polynomial-time and as such the complement of $L_{1}$ is in P.

- (10) Show that $EQ_{DFA}$ is in the class $P$.

## Answer:

Success

To demonstrate that $EQ_{DFA}$ is in $P$, we can construct a polynomial-time algorithm to solve the decision problem.

This may be done by constructing the symmetric difference automaton C from two DFAs A and B. DFA C accepts a string only if exactly one of A or B accept it. This is done in polynomial time.

Additionally, we will construct an algorithm which decides whether a language L(C) is empty or not in polynomial time.

Consider a Turing Machine M: M = “On input C which is a DFA:

- Mark the start state of C.
- Move along the transition into the next unmarked state and mark it.
- Repeat step 2 until there is no remaining and reachable unmarked states.
- If there is a path from the start state to any accept state, we accept. Otherwise reject.
The above algorithm effectively is a graph search algorithm which runs in polynomial time.

As both algorithms run in polynomial time, their sums also run in polynomial time. This shows that $EQ_{DFA}$ is in P.

- (10) $ALL_{DFA}$ = { < D > | D is a DFA with L(D) = Σ* }. Show that $ALL_{DFA}$ is in the class $P$.

## Answer:

Success

We need to show that there is a polynomial time algorithm to decide if a given DFA $D$ recognizes every string over its alphabet, this is to say if $L(D)=Σ_{∗}$.

The following is a polynomial-time algorithm which solve this decision problem:

Identify all the accept states in $D$. This can be done in linear time given the size of the DFA.

Perform a graph search algorithm starting from the start state of $D$. Mark every state that can be reached from the start state. This can also be done in linear time given the size of the DFA.

Check if all states marked in step 2 are accepting states. If they are, then $D$ accepts all possible strings. If there remains any state that is reachable from the start state and is not an accept state, then $L(D)=Σ_{∗}$.

This algorithm may be performed in polynomial time. This shows that $ALL_{DFA}$ is in $P$.

# Problem 2: The Class NP (25 points)

- (10) Show that the class NP is closed under union and concatenation.

## Answer:

Success

If we have two languages $L_{1}$ and $L_{2}$ that are in NP, we can construct two machines $M_{1}$ and $M_{2}$ to decide $L(M_{1})$ and $L(M_{2})$ in NP time.

$M_{UNION}$ = On input <$M_{1}$, $M_{2}$, s>:

- Run $M_{1}$ on $M_{2}$ or $M_{2}$ on s: this must be chosen nondeterministically.
- If it accepts, accept. Otherwise, reject.
The Nondeterministic Turing Machine (NTM) $M_{UNION}$ runs in polynomial time if the NTMs for $L_{1}$ and $L_{2}$ both run in polynomial time, which is the case because $L_{1}$ and $L_{2}$ are in NP. Therefore, $L_{1}∪L_{2}$ is also in NP.

$M_{CONCATENATION}$ = On input <$M_{1}$, $M_{2}$, s>:

- Choose a point to split the input string so that xy = s: the position to split must be chosen nondeterministically.
- Run $M_{1}$ on x.
- Run $M_{2}$ on y.
- If both instructions accept, we accept. Otherwise, we reject.
The NTM $M_{CONCATENATION}$ runs in polynomial time if the NTMs for $L_{1}$ and $L_{2}$ both run in polynomial time, which is the case because $L_{1}$ and $L_{2}$ are in NP. Therefore, $L_{1}L_{2}$ is also in NP.

This shows the class NP is closed under union and concatenation.

- (15) We say that graphs $G$ and $H$ are
**isomorphic**if the nodes of $G$ may be reordered so that it is identical to $H$. Let $ISO={⟨G,H⟩∣GandH$ are isomorphic graphs }. Show that $ISO$ is in the class $NP$.

## Answer:

Success

Let certificate C (sequence of nodes representing a path from $s$ to $t$ in graph $G$) be the reordering of G’s nodes: M = On input <G,H,C>:

- Check that C is a valid reordering of G’s nodes with no repetitions.
- Let $G_{C}$ be the reordered G.
- If $G_{C}=H$, accept, otherwise reject.
All steps within this algorithm take either polynomial or linear time. This shows that ISO is in the class NP.

# Problem 3: NP-Completeness (25 points)

A subset $S$ of the nodes of a graph $G$ is a **dominating set** if every other node of $G$ is adjacent to some node in $S$. Consider the language:

$DOMINATING-SET={⟨G,k⟩∣G$ has a dominating set with $k$ nodes }

Show that $DOMINATING−SET$ is NP-complete by reduction from $VERTEX−COVER$.

## Answer:

Success

We can show that DOMINATING-SET is NP-complete by creation a function to convert input for VERTEX-COVER into input for DOMINATING-SET.

M = On input <G,k>:

- Create graph H.
- For each non-zero vertex in G, add a vertex v to H.
- For each edge e = (u,v) in G:
- Add a vertex w to H.
- Add edges (u,v,), (u,w), and (v,w) to H.
- Output <H,K>
Our graph G has a vertex cover iff H has a dominating set. If $G_{′}$ has a vertex cover $C$ of size $k_{′}$, then $C$ is also a dominating set in $G$ of size $k$. This is because for each node $w$ that we added in $G$, it is connected to nodes $u$ and $v$ in $G_{′}$ which are in the vertex cover $C$ (by definition of vertex cover), and hence $w$ is dominated by $C$.

If $G$ has a dominating set $D$ of size $k$, then $D$ is also a vertex cover in $G_{′}$ of size $k_{′}$. This is because $D$ cannot contain any new nodes we added in $G$ (since these nodes are not connected to each other, they cannot dominate each other). Hence $D$ must be a subset of the original nodes from $G_{′}$, and since $D$ is a dominating set in $G$, it must be a vertex cover in $G_{′}$.

This reduction can be done in polynomial time, because we can iterate through all the edges in $G_{′}$ and add new nodes in $G$ in polynomial time.

Therefore, $DOMINATING−SET$ is NP-complete.

# Problem 4: NP-not-so-Completeness (30 points)

You can find the $SUBSET−SUM$ problem in our slides, and the proof of its NP-completeness in chapter 7. Let $UNARY−SSUM$ be $SUBSET−SUM$ except with all numbers represented in unary.

- (15) Why is $UNARY−SSUM$
**not**NP-complete when $SUBSET−SUM$ is?

## Answer:

Success

UNARY-SSUM is not NP-complete because of its input encoding. Due to the nature of unary notation, input size grows linearly with the numeric value that the unary input represents. This may throw off arithmetic calculuations. This is dissimilar to SUBSET-SUM which is in binary notation and has logarithmic representation size for a number.

- (15) Show that $UNARY−SSUM$ is in the class P.

## Answer:

Success

We will show that $UNARY−SSUM$ is in $P$ by providing a polynomial-time algorithm that solves this decision problem.

Let $U$ be the sum that we’re trying to achieve, and let the set $u_{1},u_{2},...,u_{n}$ represent our unary numbers. Initialize an array $dp$ of size $U+1$, where $dp[i]$ will be true if there’s a subset that adds up to $i$ and false otherwise.

Set $dp[0]$ to be true, because there’s the empty subset which always sums up to 0.

For each $u_{i}$ from $1$ to $n$, and for each $j$ from $U$ decremented to $u_{i}$, set $dp[j]$ to be true if $dp[j−u_{i}]$ is true. This means that if there’s a subset that sums up to $j−u_{i}$, then by adding $u_{i}$ to that subset, we can also sum up to $j$.

Finally, if $dp[U]$ is true, then there’s a subset that sums up to $U$, so we accept. Otherwise, we reject.

The runtime of this algorithm is $O(nU)$, where $n$ is the number of unary numbers and $U$ is the sum we are trying to achieve. The size of a unary number is proportional to the value of the number and as such we may determine that the runtime is polynomial with respect to the size of the input. This shows that $UNARY−SSUM$ is in P.

# Problem 5: A Contrasting Path (35 points)

A simple path in a graph is a path that contains no repeated vertices, and (therefore) no cycles. Let *G* be an undirected graph and consider the languages:

$SPATH={⟨G,a,b,k⟩∣Ghas a simple path of length≤kfrom a to b}$

$LPATH={⟨G,a,b,k⟩∣Ghas a simple path of length≥kfrom a to b}$

- (10) Show that
*SPATH*is in the class P.

## Answer:

Success

SPATH is in the class P since there is a polynomial time algorithm to decide if a given undirected graph $G$ has a simple path of length $≤k$ from $a$ to $b$.

This may be solved as follows:

Use the Breadth-First Search (BFS) algorithm from vertex $a$ in the graph $G$. BFS visits vertices in increasing order of their distance from $a$, this means once we reach vertex $b$, we have found a shortest path from $a$ to $b$.

If BFS reaches the vertex $b$ and the length of the path found is $≤k$, accept. If BFS has searched all vertices reachable from $a$ without finding $b$ or if the length of the path found from $a$ to $b$ is $>k$, reject.

Since BFS runs in linear-time with respect to the number of edges and vertices of the graph ($O(∣V∣+∣E∣)$ where $∣V∣$ is the the number of vertices and $∣E∣$ are the number of edges, it is a polynomial-time algorithm. This shows that $SPATH$ is in $P$.

- (25) Show that
*LPATH*is NP-complete.

## Answer:

Success

We will show that $LPATH$ is NP-complete by demonstrating that it is in NP and that it is NP-hard.

$LPATH$ is in NP: Given a sequence of nodes representing a path from $s$ to $t$ in graph $G$, we can verify in polynomial time whether the path has no repeated nodes and whether it has length at least $k$. We can do this by checking each consecutive pair of nodes in the sequence to ensure that they form an edge in $G$ and storing a set of nodes we have visited to ensure that no node appears more than once in the sequence. This shows that $LPATH$ is in NP.Now to prove that LPATH is NP-hard by reducing from the Hamiltonian Path problem, which is known to be NP-complete:

Given an instance $⟨G_{′},s_{′},t_{′}⟩$ of $HP$, we can construct an instance $⟨G,s,t,k⟩$ of $LPATH$ where $G=G_{′}$, $s=s_{′}$, $t=t_{′}$, and $k$ is the number of nodes in $G_{′}−1$.$⟨G_{′},s_{′},t_{′}⟩$ is in $HP$ iff $⟨G,s,t,k⟩$ is in $LPATH$. A Hamiltonian path in $G_{′}$ from $s_{′}$ to $t_{′}$ is a simple path that visits every node exactly once, so its length is the number of nodes in $G_{′}$ - 1, = $k$. As $k$ is the number of nodes in $G$ minus 1, if there is a simple path of length $k$ from $s$ to $t$ in $G$, this path must be a Hamiltonian path from $s_{′}$ to $t_{′}$ in $G_{′}$.

This reduction can be done in polynomial time, because we can count the number of nodes in $G_{′}$ in polynomial time.

Therefore, $LPATH$ is NP-hard.

==This shows that $LPATH$ is NP-complete.==