# Creating a graph out of a string list?

**URL:** <https://discourse.julialang.org/t/creating-a-graph-out-of-a-string-list/42233>\
**Category:** General Usage\
**Created:** [June 29, 2020, 7:41am UTC](https://discourse.julialang.org/t/creating-a-graph-out-of-a-string-list/42233 "2020-06-29T07:41:57Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Valentine\_GRAND](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/valentine_grand/32/11278_2.png) [@Valentine\_GRAND](https://discourse.julialang.org/u/Valentine_GRAND)\
**Post date:** [June 29, 2020, 7:41am UTC](https://discourse.julialang.org/t/creating-a-graph-out-of-a-string-list/42233/1 "2020-06-29T07:41:57Z")

</div>

Hello everyone,

This is my first post here, I hope I’m going to do it right  
and it’s the right place to post it 🙂  
Here’s my problem, I have a list of string:

> `[" 'DataManager' ", " 'LauncherFrame' ", " 'DataManager' ", " 'DataManager' ", " 'DataManager' ", " 'DataManager' ", " 'DataManager' ", " 'DataManager' ", " 'DataManager' ", " 'DataManager' ", " 'ToolBox' ", " 'ToolBox' ", " 'ToolBox' ", " 'ToolBox' ", " 'ToolBox' ", " 'MonoWellInterpretationView' ", " 'MonoWellInterpretationView' ", " 'MonoWellInterpretationView' ", " 'MonoWellInterpretationView' ", " 'MonoWellInterpretationView' ", " 'MonoWellInterpretationView' ", " 'MonoWellInterpretationView' ", " 'MonoWellInterpretationView' ", " 'MonoWellInterpretationView' ", " 'MonoWellInterpretationView' ", " 'MonoWellInterpretationView' ", " 'ToolBox' ", " 'DataManager' ", " 'DataManager' ", " 'DataManager' ", " 'DataManager' "]`

This list corresponds to the names of all the windows opened during a log(session ?) on the software I am working on. The order of the names is important since it corresponds to the sequences between the windows for example the window “DataManager” was opened first then “LauncherFrame” and then again DataManager etc …

I’m trying to make a directed graph with this list where the nodes are the different types of windows and the edges are the possible transitions from one window to another (for example here there would be an arrow from DataManager to LauncherFrame etc…).  
Here’s what I have done so far :

> frame = countmap(WindowsType)

Dict{Any,Int64}(" ‘MonoWellInterpretationView’ " =\> 11," ‘DataManager’ " =\> 13," ‘LauncherFrame’ " =\> 1," ‘ToolBox’ " =\> 6)

> FrameLabel=  
> for i in keys(frame)  
> FrameLabel = [FrameLabel;i]  
> end

> A = [0 0 2 3  
> 0 0 0 0  
> 1 3 0 0  
> 0 0 3 0]

```
  G = DiGraph(A)
  # save to png

```

> draw(PNG(“graph.png”, 100, 100),gplot(G, nodelabel=FrameLabel))

![graph](https://global.discourse-cdn.com/julialang/original/3X/2/1/2185a105484b35f737bc0c8002bb288b595ce013.png)

As you can see I managed to create a graph with arrows and with a node for each type of window, the problem is the matrix A of the edges. How could I “automate” it without having to create it “by hand” for each string list that I will have? **How could I create this matrix A (for edges) directly from my list of string** with inside the transitions between the windows and their frequency for each type of transitions?

I hope I have been clear and not asked a stupid question !

Thank you in advance for your answers,

Have a good day, Valentine  
PS : Sorry if my english is bad 😅

---

<div class="post-metadata">

**Author:** ![bernhard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bernhard/32/2619_2.png) [@bernhard](https://discourse.julialang.org/u/bernhard)\
**Post date:** [June 29, 2020, 8:07am UTC](https://discourse.julialang.org/t/creating-a-graph-out-of-a-string-list/42233/2 "2020-06-29T08:07:10Z")

</div>

I only understand parts of your question as I am not too familiar with directed graphs.  
Also, I is not clear to me how the values in the matrix A are defined.

But one thing you can do is the snippet below.  
I though that `countmap(transitions)` would be similar (or equal) to A, but it is not…

```julia
list=[" 'DataManager' ", " 'LauncherFrame' ", " 'DataManager' ", " 'DataManager' ", " 'DataManager' ", " 'DataManager' ", " 'DataManager' ", " 'DataManager' ", " 'DataManager' ", " 'DataManager' ", " 'ToolBox' ", " 'ToolBox' ", " 'ToolBox' ", " 'ToolBox' ", " 'ToolBox' ", " 'MonoWellInterpretationView' ", " 'MonoWellInterpretationView' ", " 'MonoWellInterpretationView' ", " 'MonoWellInterpretationView' ", " 'MonoWellInterpretationView' ", " 'MonoWellInterpretationView' ", " 'MonoWellInterpretationView' ", " 'MonoWellInterpretationView' ", " 'MonoWellInterpretationView' ", " 'MonoWellInterpretationView' ", " 'MonoWellInterpretationView' ", " 'ToolBox' ", " 'DataManager' ", " 'DataManager' ", " 'DataManager' ", " 'DataManager' "]

function get_transitions(list)
    transitions = Vector{Vector{String}}()
    for i=2:length(list)
        push!(transitions,list[i-1:i])
    end
    return transitions
end

transitions = get_transitions(list)

countmap(transitions)
unique(list) 

julia> countmap(transitions)
Dict{Array{String,1},Int64} with 9 entries:
  [" 'ToolBox' ", " 'ToolBox' "] => 4
  [" 'LauncherFrame' ", " 'DataManager' "] => 1
  [" 'MonoWellInterpretationView' ", " 'MonoWellInterpretationView' "] => 10
  [" 'ToolBox' ", " 'MonoWellInterpretationView' "] => 1
  [" 'MonoWellInterpretationView' ", " 'ToolBox' "] => 1
  [" 'DataManager' ", " 'LauncherFrame' "] => 1
  [" 'DataManager' ", " 'ToolBox' "] => 1
  [" 'DataManager' ", " 'DataManager' "] => 10
  [" 'ToolBox' ", " 'DataManager' "] => 1

julia> unique(list)
4-element Array{String,1}:
 " 'DataManager' "
 " 'LauncherFrame' "
 " 'ToolBox' "
 " 'MonoWellInterpretationView' "

```

---

<div class="post-metadata">

**Author:** ![Valentine\_GRAND](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/valentine_grand/32/11278_2.png) [@Valentine\_GRAND](https://discourse.julialang.org/u/Valentine_GRAND)\
**Post date:** [June 29, 2020, 8:21am UTC](https://discourse.julialang.org/t/creating-a-graph-out-of-a-string-list/42233/3 "2020-06-29T08:21:44Z")

</div>

Thank you very much for your answer and sorry if it is not clear !  
Each value in the matrix A corresponds to the number of times a type of transition occurs. These are the values you found with the “countmap(transitions)”.

---

<div class="post-metadata">

**Author:** ![bernhard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bernhard/32/2619_2.png) [@bernhard](https://discourse.julialang.org/u/bernhard)\
**Post date:** [June 29, 2020, 8:28am UTC](https://discourse.julialang.org/t/creating-a-graph-out-of-a-string-list/42233/4 "2020-06-29T08:28:47Z")

</div>

> [@Valentine\_GRAND](#):
>
> A = [0 0 2 3  
> 0 0 0 0  
> 1 3 0 0  
> 0 0 3 0]

ah ok. I was irritated as your Matrix A has values “0 0 2 3” on the first line. But my result does not show these values.

---

<div class="post-metadata">

**Author:** ![Valentine\_GRAND](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/valentine_grand/32/11278_2.png) [@Valentine\_GRAND](https://discourse.julialang.org/u/Valentine_GRAND)\
**Post date:** [June 29, 2020, 8:31am UTC](https://discourse.julialang.org/t/creating-a-graph-out-of-a-string-list/42233/5 "2020-06-29T08:31:10Z")

</div>

Yes, it’s my fault I didn’t specify that they were random values just to make a graph appear.

---

<div class="post-metadata">

**Author:** ![tomerarnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomerarnon/32/3170_2.png) [@tomerarnon](https://discourse.julialang.org/u/tomerarnon)\
**Post date:** [June 29, 2020, 8:35am UTC](https://discourse.julialang.org/t/creating-a-graph-out-of-a-string-list/42233/6 "2020-06-29T08:35:40Z")

</div>

Here’s one way to get the adjacency matrix:

```julia
str = [" 'DataManager' ", " 'LauncherFrame' ", " 'DataManager' ", " 'DataManager' ", " 'DataManager' ", " 'DataManager' ", " 'DataManager' ", " 'DataManager' ", " 'DataManager' ", " 'DataManager' ", " 'ToolBox' ", " 'ToolBox' ", " 'ToolBox' ", " 'ToolBox' ", " 'ToolBox' ", " 'MonoWellInterpretationView' ", " 'MonoWellInterpretationView' ", " 'MonoWellInterpretationView' ", " 'MonoWellInterpretationView' ", " 'MonoWellInterpretationView' ", " 'MonoWellInterpretationView' ", " 'MonoWellInterpretationView' ", " 'MonoWellInterpretationView' ", " 'MonoWellInterpretationView' ", " 'MonoWellInterpretationView' ", " 'MonoWellInterpretationView' ", " 'ToolBox' ", " 'DataManager' ", " 'DataManager' ", " 'DataManager' ", " 'DataManager' "]

d = Dict(unique(str) .=> 1:length(unique(str)))
A = zeros(Int, length(d), length(d))
for i in 1:length(str)-1
    A[d[str[i]], d[str[i+1]]] += 1
end

```

---

<div class="post-metadata">

**Author:** ![Valentine\_GRAND](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/valentine_grand/32/11278_2.png) [@Valentine\_GRAND](https://discourse.julialang.org/u/Valentine_GRAND)\
**Post date:** [June 29, 2020, 8:58am UTC](https://discourse.julialang.org/t/creating-a-graph-out-of-a-string-list/42233/7 "2020-06-29T08:58:25Z")

</div>

Thank you very much , this is exactly what i needed !
