TITLE: Is there a monoid structure on the set of paths of a graph?
QUESTION [8 upvotes]: Given a graph G, and the set of paths in G called PathG.
Is there a monoid structure on PathG? Will concatenation be the multiplication formula? even if it's not defined for some paths? 
What about the identity?

REPLY [11 votes]: Eric's answer is correct, but there's a lot more to be said here.
Firstly, there's no need to start with a graph for this to work; we can start with an arbitrary digraph. Unfortunately, the term 'digraph' is a little ambiguous, so I prefer to call these things quivers. It basically means 'digraph, possibly with loops, possibly with multiple edges.'
Now to every quiver $X$, we can assign a category $\mathrm{Path}(X)$ as follows: the objects are just $X$-objects, the morphisms are composable sequences of $X$-morphisms, composition is by concatenation of sequences, and identities are empty sequences.
This process is functorial: given a morphism of quivers $$f:X \rightarrow Y,$$ we get a corresponding morphism of categories (i.e. a functor) $$\mathrm{Path}(f) : \mathrm{Path}(X) \rightarrow \mathrm{Path}(Y).$$ In other words, we have a functor $$\mathrm{Path} : \mathbf{Quiv} \rightarrow \mathbf{Cat}.$$ It turns out to be left-adjoint to the underlying quiver functor $\mathbf{Cat} \rightarrow \mathbf{Quiv}.$ This proves that the underlying quiver functor preserves limits, and that $\mathrm{Path}$ preserves colimits.
So the 'category of paths' construction can also be referred to as the 'free category on a quiver' construction.
Now suppose $S$ is a set. Then there's a free monoid on $S$, usually denoted $S^*$. This can be defined as the set of all finite words in the alphabet $S$. But note that $S^*$ isn't just a monoid; its actually an involutive monoid, with involution given by reversal of words. The involution satisfies $$(ab)^\dagger = b^\dagger a^\dagger, \qquad 1^\dagger = 1.$$
Involutive monoids should really be called 'dagger monoids', in my opinion, because there's also monoids equipped with an involution $x \mapsto \overline{x}$ satisfying $$\overline{xy} = \overline{x}\overline{y}, \qquad \overline{1} = 1,$$ and it seems unfair that these aren't 'involutive monoids' under the usual definition. 
Anyway, in a similar way, $\mathrm{Path}(X)$ isn't just a category; in fact, its always a dagger category. (This is good terminology!)
Furthermore, $\mathrm{Path}(f)$ plays nice with the dagger structure. Hence, we get a functor $$\mathrm{Path} : \mathbf{Quiv} \rightarrow \mathbf{DagCat}.$$ Be wary: the above functor isn't left-adjoint to the forgetful functor $\mathbf{DagCat} \rightarrow \mathbf{Quiv}$.
Finally, note that $\mathrm{Path}$ generalizes the free monoid construction. It goes like this:

given a set $S$, we can build a quiver $\dot{S}$ with one object, and a morphism for each element of $S$.
given a monoid $M$, we can build a category $\mathbb{B}M$ with one object, and a morphism for each element of $M$. Composition of morphisms is given by the multiplication operation of $M$; the identity morphism is given by $1_M$.

Its clear that $$\mathrm{Path}(\dot{S}) \cong \mathbb{B}(S^*).$$ Hence, since $\mathbb{B}$ is an embedding of categories, we can write $$S^* \cong \mathbb{B}^{-1}(\mathrm{Path}(\dot{S}))$$ and thereby get free monoids as a special case of the $\mathrm{Path}$ construction.