Typesetting a directed, weighted graph with TikZ












21















I just started doing things with TikZ today and I run into a problem: there is just no example code snippets for typesetting directed, weighted graphs. Can anyone supply one simple example in an answer?










share|improve this question

























  • By the way, is there anything like that on TeXample?

    – Torbjørn T.
    Dec 5 '11 at 14:10











  • Have a look at: texample.net/tikz/examples/prims-algorithm and texample.net/tikz/examples/state-machine. The first one is not directed but can easily be made so. The second is a state machine, but replacing the edge labels should be straightforward and you obtain a directed weighted graph. And then there is: texample.net/tikz/examples/tkz-berge but I don't know the status of tkz-berge. Have a look at the rest of the site as well, many great examples.

    – Roelof Spijker
    Dec 5 '11 at 14:11













  • I couldn't seem to find anything on tkz-berge. Perhaps it's been retired. Is Altermundus about?

    – qubyte
    Dec 5 '11 at 14:14











  • If I'm not mistaking, you could something like that in R, and include the graphic either via a pdf, or automatically via Sweave.

    – Roman Luštrik
    Dec 5 '11 at 14:16











  • Ah! Speak of the devil!

    – qubyte
    Dec 5 '11 at 14:17
















21















I just started doing things with TikZ today and I run into a problem: there is just no example code snippets for typesetting directed, weighted graphs. Can anyone supply one simple example in an answer?










share|improve this question

























  • By the way, is there anything like that on TeXample?

    – Torbjørn T.
    Dec 5 '11 at 14:10











  • Have a look at: texample.net/tikz/examples/prims-algorithm and texample.net/tikz/examples/state-machine. The first one is not directed but can easily be made so. The second is a state machine, but replacing the edge labels should be straightforward and you obtain a directed weighted graph. And then there is: texample.net/tikz/examples/tkz-berge but I don't know the status of tkz-berge. Have a look at the rest of the site as well, many great examples.

    – Roelof Spijker
    Dec 5 '11 at 14:11













  • I couldn't seem to find anything on tkz-berge. Perhaps it's been retired. Is Altermundus about?

    – qubyte
    Dec 5 '11 at 14:14











  • If I'm not mistaking, you could something like that in R, and include the graphic either via a pdf, or automatically via Sweave.

    – Roman Luštrik
    Dec 5 '11 at 14:16











  • Ah! Speak of the devil!

    – qubyte
    Dec 5 '11 at 14:17














21












21








21


16






I just started doing things with TikZ today and I run into a problem: there is just no example code snippets for typesetting directed, weighted graphs. Can anyone supply one simple example in an answer?










share|improve this question
















I just started doing things with TikZ today and I run into a problem: there is just no example code snippets for typesetting directed, weighted graphs. Can anyone supply one simple example in an answer?







tikz-pgf graphs






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 5 '11 at 14:50









Yossi Farjoun

8,06695890




8,06695890










asked Dec 5 '11 at 14:04









user1049393user1049393

2221310




2221310













  • By the way, is there anything like that on TeXample?

    – Torbjørn T.
    Dec 5 '11 at 14:10











  • Have a look at: texample.net/tikz/examples/prims-algorithm and texample.net/tikz/examples/state-machine. The first one is not directed but can easily be made so. The second is a state machine, but replacing the edge labels should be straightforward and you obtain a directed weighted graph. And then there is: texample.net/tikz/examples/tkz-berge but I don't know the status of tkz-berge. Have a look at the rest of the site as well, many great examples.

    – Roelof Spijker
    Dec 5 '11 at 14:11













  • I couldn't seem to find anything on tkz-berge. Perhaps it's been retired. Is Altermundus about?

    – qubyte
    Dec 5 '11 at 14:14











  • If I'm not mistaking, you could something like that in R, and include the graphic either via a pdf, or automatically via Sweave.

    – Roman Luštrik
    Dec 5 '11 at 14:16











  • Ah! Speak of the devil!

    – qubyte
    Dec 5 '11 at 14:17



















  • By the way, is there anything like that on TeXample?

    – Torbjørn T.
    Dec 5 '11 at 14:10











  • Have a look at: texample.net/tikz/examples/prims-algorithm and texample.net/tikz/examples/state-machine. The first one is not directed but can easily be made so. The second is a state machine, but replacing the edge labels should be straightforward and you obtain a directed weighted graph. And then there is: texample.net/tikz/examples/tkz-berge but I don't know the status of tkz-berge. Have a look at the rest of the site as well, many great examples.

    – Roelof Spijker
    Dec 5 '11 at 14:11













  • I couldn't seem to find anything on tkz-berge. Perhaps it's been retired. Is Altermundus about?

    – qubyte
    Dec 5 '11 at 14:14











  • If I'm not mistaking, you could something like that in R, and include the graphic either via a pdf, or automatically via Sweave.

    – Roman Luštrik
    Dec 5 '11 at 14:16











  • Ah! Speak of the devil!

    – qubyte
    Dec 5 '11 at 14:17

















By the way, is there anything like that on TeXample?

– Torbjørn T.
Dec 5 '11 at 14:10





By the way, is there anything like that on TeXample?

– Torbjørn T.
Dec 5 '11 at 14:10













Have a look at: texample.net/tikz/examples/prims-algorithm and texample.net/tikz/examples/state-machine. The first one is not directed but can easily be made so. The second is a state machine, but replacing the edge labels should be straightforward and you obtain a directed weighted graph. And then there is: texample.net/tikz/examples/tkz-berge but I don't know the status of tkz-berge. Have a look at the rest of the site as well, many great examples.

– Roelof Spijker
Dec 5 '11 at 14:11







Have a look at: texample.net/tikz/examples/prims-algorithm and texample.net/tikz/examples/state-machine. The first one is not directed but can easily be made so. The second is a state machine, but replacing the edge labels should be straightforward and you obtain a directed weighted graph. And then there is: texample.net/tikz/examples/tkz-berge but I don't know the status of tkz-berge. Have a look at the rest of the site as well, many great examples.

– Roelof Spijker
Dec 5 '11 at 14:11















I couldn't seem to find anything on tkz-berge. Perhaps it's been retired. Is Altermundus about?

– qubyte
Dec 5 '11 at 14:14





I couldn't seem to find anything on tkz-berge. Perhaps it's been retired. Is Altermundus about?

– qubyte
Dec 5 '11 at 14:14













If I'm not mistaking, you could something like that in R, and include the graphic either via a pdf, or automatically via Sweave.

– Roman Luštrik
Dec 5 '11 at 14:16





If I'm not mistaking, you could something like that in R, and include the graphic either via a pdf, or automatically via Sweave.

– Roman Luštrik
Dec 5 '11 at 14:16













Ah! Speak of the devil!

– qubyte
Dec 5 '11 at 14:17





Ah! Speak of the devil!

– qubyte
Dec 5 '11 at 14:17










2 Answers
2






active

oldest

votes


















32














I made tkz-graph and tkz-berge to help beginners to draw some graphs. tkz-berge is used for specials graphs (named graphs in graph theory)



You can use only tikz to draw graphs.
Version with tkz-graph



documentclass{article}
usepackage[utf8]{inputenc}
usepackage[usenames,dvipsnames]{xcolor}
usepackage{fullpage}
usepackage[upright]{fourier}
usepackage{tkz-graph}
usetikzlibrary{arrows}
thispagestyle{empty}
begin{document}
SetVertexNormal[Shape = circle,
FillColor = orange,
LineWidth = 2pt]
SetUpEdge[lw = 1.5pt,
color = black,
labelcolor = white,
labeltext = red,
labelstyle = {sloped,draw,text=blue}]
begin{center}
begin{tikzpicture}
Vertex[x=0 ,y=0]{K}
Vertex[x=0 ,y=2]{F}
Vertex[x=-1,y=4]{D}
Vertex[x=3 ,y=7]{H}
Vertex[x=8 ,y=5]{B}
Vertex[x=9 ,y=2]{N}
Vertex[x=5 ,y=0]{M}
Vertex[x=3 ,y=1]{S}
tikzset{EdgeStyle/.append style = {bend left}}
Edge[label = $120$](K)(F)
Edge[label = $650$](H)(S)
Edge[label = $780$](H)(M)
Edge[label = $490$](D)(B)
Edge[label = $600$](D)(M)
Edge[label = $580$](B)(M)
Edge[label = $600$](H)(N)
Edge[label = $490$](F)(H)
tikzset{EdgeStyle/.append style = {bend right}}
Edge[label = $630$](S)(B)
Edge[label = $210$](S)(N)
Edge[label = $230$](S)(M)
end{tikzpicture}
end{center}

end{document}


enter image description here



With arrows on edges



documentclass[11pt]{scrartcl}
usepackage{tkz-graph}


begin{document}
begin{tikzpicture}
SetUpEdge[lw = 1.5pt,
color = orange,
labelcolor = white]
GraphInit[vstyle=Normal]
SetGraphUnit{3}
tikzset{VertexStyle/.append style={fill}}
Vertex{P}
NOEA(P){B} SOEA(P){M} NOEA(B){D}
SOEA(B){C} SOEA(C){L}
tikzset{EdgeStyle/.style={->}}
Edge[label=$3$](C)(B)
Edge[label=$10$](D)(B)
Edge[label=$10$](L)(M)
Edge[label=$10$](B)(P)
tikzset{EdgeStyle/.style={<->}}
Edge[label=$4$](P)(M)
Edge[label=$9$](C)(M)
Edge[label=$4$](C)(L)
Edge[label=$5$](C)(D)
Edge[label=$10$](B)(M)
tikzset{EdgeStyle/.style={<->,relative=false,in=0,out=60}}
Edge[label=$11$](L)(D)
end{tikzpicture}
end{document}


enter image description here



update : Version with tikz and automata



documentclass[11pt]{scrartcl} 
PassOptionsToPackage{usenames,dvipsnames,svgnames}{xcolor}
usepackage{tikz}
usetikzlibrary{arrows,positioning,automata}

begin{document}
begin{tikzpicture}[>=stealth',shorten >=1pt,node distance=3cm,on grid,initial/.style ={}]
node[state] (P) {$P$};
node[state] (B) [above right =of P] {$B$};
node[state] (M) [below right =of P] {$M$};
node[state] (D) [above right =of B] {$D$};
node[state] (C) [below right =of B] {$C$};
node[state] (L) [below right =of C] {$L$};
tikzset{mystyle/.style={->,double=orange}}
tikzset{every node/.style={fill=white}}
path (C) edge [mystyle] node {$3$} (B)
(D) edge [mystyle] node {$10$} (B)
(L) edge [mystyle] node {$10$} (M)
(B) edge [mystyle] node {$10$} (P);
tikzset{mystyle/.style={<->,double=orange}}
path (P) edge [mystyle] node {$4$} (M)
(C) edge [mystyle] node {$9$} (M)
(C) edge [mystyle] node {$4$} (D)
(B) edge [mystyle] node {$5$} (M);
tikzset{mystyle/.style={<->,relative=false,in=0,out=60,double=orange}}
path (L) edge [mystyle] node {$10$} (D);
end{tikzpicture}
end{document}


enter image description here






share|improve this answer





















  • 1





    It might be nice to include where these packages can be found (link) and on a personal note, is there any documentation in English? My French is not what it used to be in high school and even then it was pretty bad. Also, it may be better to add a directed example here, since the question specifically asks for it.

    – Roelof Spijker
    Dec 5 '11 at 14:26











  • TeXlive 2011, MikTeX or here ctan.org/tex-archive/macros/latex/contrib/tkz/tkz-graph. With the next version, there will be a French version. Count Zero will help me to translate ( tex.stackexchange.com/questions/35738/… ). You have some examples on my site altermundus.com/pages/tkz/graph/index.html

    – Alain Matthes
    Dec 5 '11 at 15:49













  • That's almost what I need and I have typeset a similar undirected weighted graph using "Prim's algorithm" example from texample.net. Although, I need to include somehow a direction for each edge in the graph. Frankly, the edges should be arrows pointing from a source vertex to a destination vertex rather than simply connecting the two.

    – user1049393
    Dec 6 '11 at 11:54











  • I added an example with arrows on edges. You need to study tikz to modify sometimes the style of edges or vertices.

    – Alain Matthes
    Dec 6 '11 at 14:48













  • Thanks for your input, Altermundus, but I cannot accept your answer as I have a lot of code purely in TikZ/PGF, and, thus, cannot afford porting to tkz-graph. Frankly, I ended up using 'automata' - TikZ-library in order to draw directed, weighted graphs (one, e.g., represented by the second image in your answer).

    – user1049393
    Dec 9 '11 at 18:00



















0














Here is a stripped down version usking tkz-graph of Alain Matthes's code that provides reasonable looking (for CS paper) defaults.



documentclass{article}
usepackage{tkz-graph}

begin{document}

begin{figure}
centering
begin{tikzpicture}[scale=0.75,transform shape]
Vertex[x=0,y=0]{K}
Vertex[x=0,y=2]{F}
Vertex[x=-1,y=4]{D}
Vertex[x=3,y=7]{H}
Vertex[x=8,y=5]{B}
Vertex[x=9,y=2]{N}
Vertex[x=5,y=0]{M}
Vertex[x=3,y=3]{S}
tikzstyle{LabelStyle}=[fill=white,sloped]
tikzstyle{EdgeStyle}=
Edge[label=$120$](K)(F)
Edge[label=$650$](H)(S)
Edge[label=$780$](H)(M)
Edge[label=$490$](D)(B)
Edge[label=$600$](D)(M)
Edge[label=$580$](B)(M)
Edge[label=$600$](H)(N)
Edge[label=$490$](F)(H)
tikzstyle{EdgeStyle}=[pre, bend right]
Edge[label=$210$](S)(N)
Edge[label=$230$](S)(M)
end{tikzpicture}
end{figure}

end{document}


example





share























    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "85"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f37185%2ftypesetting-a-directed-weighted-graph-with-tikz%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    32














    I made tkz-graph and tkz-berge to help beginners to draw some graphs. tkz-berge is used for specials graphs (named graphs in graph theory)



    You can use only tikz to draw graphs.
    Version with tkz-graph



    documentclass{article}
    usepackage[utf8]{inputenc}
    usepackage[usenames,dvipsnames]{xcolor}
    usepackage{fullpage}
    usepackage[upright]{fourier}
    usepackage{tkz-graph}
    usetikzlibrary{arrows}
    thispagestyle{empty}
    begin{document}
    SetVertexNormal[Shape = circle,
    FillColor = orange,
    LineWidth = 2pt]
    SetUpEdge[lw = 1.5pt,
    color = black,
    labelcolor = white,
    labeltext = red,
    labelstyle = {sloped,draw,text=blue}]
    begin{center}
    begin{tikzpicture}
    Vertex[x=0 ,y=0]{K}
    Vertex[x=0 ,y=2]{F}
    Vertex[x=-1,y=4]{D}
    Vertex[x=3 ,y=7]{H}
    Vertex[x=8 ,y=5]{B}
    Vertex[x=9 ,y=2]{N}
    Vertex[x=5 ,y=0]{M}
    Vertex[x=3 ,y=1]{S}
    tikzset{EdgeStyle/.append style = {bend left}}
    Edge[label = $120$](K)(F)
    Edge[label = $650$](H)(S)
    Edge[label = $780$](H)(M)
    Edge[label = $490$](D)(B)
    Edge[label = $600$](D)(M)
    Edge[label = $580$](B)(M)
    Edge[label = $600$](H)(N)
    Edge[label = $490$](F)(H)
    tikzset{EdgeStyle/.append style = {bend right}}
    Edge[label = $630$](S)(B)
    Edge[label = $210$](S)(N)
    Edge[label = $230$](S)(M)
    end{tikzpicture}
    end{center}

    end{document}


    enter image description here



    With arrows on edges



    documentclass[11pt]{scrartcl}
    usepackage{tkz-graph}


    begin{document}
    begin{tikzpicture}
    SetUpEdge[lw = 1.5pt,
    color = orange,
    labelcolor = white]
    GraphInit[vstyle=Normal]
    SetGraphUnit{3}
    tikzset{VertexStyle/.append style={fill}}
    Vertex{P}
    NOEA(P){B} SOEA(P){M} NOEA(B){D}
    SOEA(B){C} SOEA(C){L}
    tikzset{EdgeStyle/.style={->}}
    Edge[label=$3$](C)(B)
    Edge[label=$10$](D)(B)
    Edge[label=$10$](L)(M)
    Edge[label=$10$](B)(P)
    tikzset{EdgeStyle/.style={<->}}
    Edge[label=$4$](P)(M)
    Edge[label=$9$](C)(M)
    Edge[label=$4$](C)(L)
    Edge[label=$5$](C)(D)
    Edge[label=$10$](B)(M)
    tikzset{EdgeStyle/.style={<->,relative=false,in=0,out=60}}
    Edge[label=$11$](L)(D)
    end{tikzpicture}
    end{document}


    enter image description here



    update : Version with tikz and automata



    documentclass[11pt]{scrartcl} 
    PassOptionsToPackage{usenames,dvipsnames,svgnames}{xcolor}
    usepackage{tikz}
    usetikzlibrary{arrows,positioning,automata}

    begin{document}
    begin{tikzpicture}[>=stealth',shorten >=1pt,node distance=3cm,on grid,initial/.style ={}]
    node[state] (P) {$P$};
    node[state] (B) [above right =of P] {$B$};
    node[state] (M) [below right =of P] {$M$};
    node[state] (D) [above right =of B] {$D$};
    node[state] (C) [below right =of B] {$C$};
    node[state] (L) [below right =of C] {$L$};
    tikzset{mystyle/.style={->,double=orange}}
    tikzset{every node/.style={fill=white}}
    path (C) edge [mystyle] node {$3$} (B)
    (D) edge [mystyle] node {$10$} (B)
    (L) edge [mystyle] node {$10$} (M)
    (B) edge [mystyle] node {$10$} (P);
    tikzset{mystyle/.style={<->,double=orange}}
    path (P) edge [mystyle] node {$4$} (M)
    (C) edge [mystyle] node {$9$} (M)
    (C) edge [mystyle] node {$4$} (D)
    (B) edge [mystyle] node {$5$} (M);
    tikzset{mystyle/.style={<->,relative=false,in=0,out=60,double=orange}}
    path (L) edge [mystyle] node {$10$} (D);
    end{tikzpicture}
    end{document}


    enter image description here






    share|improve this answer





















    • 1





      It might be nice to include where these packages can be found (link) and on a personal note, is there any documentation in English? My French is not what it used to be in high school and even then it was pretty bad. Also, it may be better to add a directed example here, since the question specifically asks for it.

      – Roelof Spijker
      Dec 5 '11 at 14:26











    • TeXlive 2011, MikTeX or here ctan.org/tex-archive/macros/latex/contrib/tkz/tkz-graph. With the next version, there will be a French version. Count Zero will help me to translate ( tex.stackexchange.com/questions/35738/… ). You have some examples on my site altermundus.com/pages/tkz/graph/index.html

      – Alain Matthes
      Dec 5 '11 at 15:49













    • That's almost what I need and I have typeset a similar undirected weighted graph using "Prim's algorithm" example from texample.net. Although, I need to include somehow a direction for each edge in the graph. Frankly, the edges should be arrows pointing from a source vertex to a destination vertex rather than simply connecting the two.

      – user1049393
      Dec 6 '11 at 11:54











    • I added an example with arrows on edges. You need to study tikz to modify sometimes the style of edges or vertices.

      – Alain Matthes
      Dec 6 '11 at 14:48













    • Thanks for your input, Altermundus, but I cannot accept your answer as I have a lot of code purely in TikZ/PGF, and, thus, cannot afford porting to tkz-graph. Frankly, I ended up using 'automata' - TikZ-library in order to draw directed, weighted graphs (one, e.g., represented by the second image in your answer).

      – user1049393
      Dec 9 '11 at 18:00
















    32














    I made tkz-graph and tkz-berge to help beginners to draw some graphs. tkz-berge is used for specials graphs (named graphs in graph theory)



    You can use only tikz to draw graphs.
    Version with tkz-graph



    documentclass{article}
    usepackage[utf8]{inputenc}
    usepackage[usenames,dvipsnames]{xcolor}
    usepackage{fullpage}
    usepackage[upright]{fourier}
    usepackage{tkz-graph}
    usetikzlibrary{arrows}
    thispagestyle{empty}
    begin{document}
    SetVertexNormal[Shape = circle,
    FillColor = orange,
    LineWidth = 2pt]
    SetUpEdge[lw = 1.5pt,
    color = black,
    labelcolor = white,
    labeltext = red,
    labelstyle = {sloped,draw,text=blue}]
    begin{center}
    begin{tikzpicture}
    Vertex[x=0 ,y=0]{K}
    Vertex[x=0 ,y=2]{F}
    Vertex[x=-1,y=4]{D}
    Vertex[x=3 ,y=7]{H}
    Vertex[x=8 ,y=5]{B}
    Vertex[x=9 ,y=2]{N}
    Vertex[x=5 ,y=0]{M}
    Vertex[x=3 ,y=1]{S}
    tikzset{EdgeStyle/.append style = {bend left}}
    Edge[label = $120$](K)(F)
    Edge[label = $650$](H)(S)
    Edge[label = $780$](H)(M)
    Edge[label = $490$](D)(B)
    Edge[label = $600$](D)(M)
    Edge[label = $580$](B)(M)
    Edge[label = $600$](H)(N)
    Edge[label = $490$](F)(H)
    tikzset{EdgeStyle/.append style = {bend right}}
    Edge[label = $630$](S)(B)
    Edge[label = $210$](S)(N)
    Edge[label = $230$](S)(M)
    end{tikzpicture}
    end{center}

    end{document}


    enter image description here



    With arrows on edges



    documentclass[11pt]{scrartcl}
    usepackage{tkz-graph}


    begin{document}
    begin{tikzpicture}
    SetUpEdge[lw = 1.5pt,
    color = orange,
    labelcolor = white]
    GraphInit[vstyle=Normal]
    SetGraphUnit{3}
    tikzset{VertexStyle/.append style={fill}}
    Vertex{P}
    NOEA(P){B} SOEA(P){M} NOEA(B){D}
    SOEA(B){C} SOEA(C){L}
    tikzset{EdgeStyle/.style={->}}
    Edge[label=$3$](C)(B)
    Edge[label=$10$](D)(B)
    Edge[label=$10$](L)(M)
    Edge[label=$10$](B)(P)
    tikzset{EdgeStyle/.style={<->}}
    Edge[label=$4$](P)(M)
    Edge[label=$9$](C)(M)
    Edge[label=$4$](C)(L)
    Edge[label=$5$](C)(D)
    Edge[label=$10$](B)(M)
    tikzset{EdgeStyle/.style={<->,relative=false,in=0,out=60}}
    Edge[label=$11$](L)(D)
    end{tikzpicture}
    end{document}


    enter image description here



    update : Version with tikz and automata



    documentclass[11pt]{scrartcl} 
    PassOptionsToPackage{usenames,dvipsnames,svgnames}{xcolor}
    usepackage{tikz}
    usetikzlibrary{arrows,positioning,automata}

    begin{document}
    begin{tikzpicture}[>=stealth',shorten >=1pt,node distance=3cm,on grid,initial/.style ={}]
    node[state] (P) {$P$};
    node[state] (B) [above right =of P] {$B$};
    node[state] (M) [below right =of P] {$M$};
    node[state] (D) [above right =of B] {$D$};
    node[state] (C) [below right =of B] {$C$};
    node[state] (L) [below right =of C] {$L$};
    tikzset{mystyle/.style={->,double=orange}}
    tikzset{every node/.style={fill=white}}
    path (C) edge [mystyle] node {$3$} (B)
    (D) edge [mystyle] node {$10$} (B)
    (L) edge [mystyle] node {$10$} (M)
    (B) edge [mystyle] node {$10$} (P);
    tikzset{mystyle/.style={<->,double=orange}}
    path (P) edge [mystyle] node {$4$} (M)
    (C) edge [mystyle] node {$9$} (M)
    (C) edge [mystyle] node {$4$} (D)
    (B) edge [mystyle] node {$5$} (M);
    tikzset{mystyle/.style={<->,relative=false,in=0,out=60,double=orange}}
    path (L) edge [mystyle] node {$10$} (D);
    end{tikzpicture}
    end{document}


    enter image description here






    share|improve this answer





















    • 1





      It might be nice to include where these packages can be found (link) and on a personal note, is there any documentation in English? My French is not what it used to be in high school and even then it was pretty bad. Also, it may be better to add a directed example here, since the question specifically asks for it.

      – Roelof Spijker
      Dec 5 '11 at 14:26











    • TeXlive 2011, MikTeX or here ctan.org/tex-archive/macros/latex/contrib/tkz/tkz-graph. With the next version, there will be a French version. Count Zero will help me to translate ( tex.stackexchange.com/questions/35738/… ). You have some examples on my site altermundus.com/pages/tkz/graph/index.html

      – Alain Matthes
      Dec 5 '11 at 15:49













    • That's almost what I need and I have typeset a similar undirected weighted graph using "Prim's algorithm" example from texample.net. Although, I need to include somehow a direction for each edge in the graph. Frankly, the edges should be arrows pointing from a source vertex to a destination vertex rather than simply connecting the two.

      – user1049393
      Dec 6 '11 at 11:54











    • I added an example with arrows on edges. You need to study tikz to modify sometimes the style of edges or vertices.

      – Alain Matthes
      Dec 6 '11 at 14:48













    • Thanks for your input, Altermundus, but I cannot accept your answer as I have a lot of code purely in TikZ/PGF, and, thus, cannot afford porting to tkz-graph. Frankly, I ended up using 'automata' - TikZ-library in order to draw directed, weighted graphs (one, e.g., represented by the second image in your answer).

      – user1049393
      Dec 9 '11 at 18:00














    32












    32








    32







    I made tkz-graph and tkz-berge to help beginners to draw some graphs. tkz-berge is used for specials graphs (named graphs in graph theory)



    You can use only tikz to draw graphs.
    Version with tkz-graph



    documentclass{article}
    usepackage[utf8]{inputenc}
    usepackage[usenames,dvipsnames]{xcolor}
    usepackage{fullpage}
    usepackage[upright]{fourier}
    usepackage{tkz-graph}
    usetikzlibrary{arrows}
    thispagestyle{empty}
    begin{document}
    SetVertexNormal[Shape = circle,
    FillColor = orange,
    LineWidth = 2pt]
    SetUpEdge[lw = 1.5pt,
    color = black,
    labelcolor = white,
    labeltext = red,
    labelstyle = {sloped,draw,text=blue}]
    begin{center}
    begin{tikzpicture}
    Vertex[x=0 ,y=0]{K}
    Vertex[x=0 ,y=2]{F}
    Vertex[x=-1,y=4]{D}
    Vertex[x=3 ,y=7]{H}
    Vertex[x=8 ,y=5]{B}
    Vertex[x=9 ,y=2]{N}
    Vertex[x=5 ,y=0]{M}
    Vertex[x=3 ,y=1]{S}
    tikzset{EdgeStyle/.append style = {bend left}}
    Edge[label = $120$](K)(F)
    Edge[label = $650$](H)(S)
    Edge[label = $780$](H)(M)
    Edge[label = $490$](D)(B)
    Edge[label = $600$](D)(M)
    Edge[label = $580$](B)(M)
    Edge[label = $600$](H)(N)
    Edge[label = $490$](F)(H)
    tikzset{EdgeStyle/.append style = {bend right}}
    Edge[label = $630$](S)(B)
    Edge[label = $210$](S)(N)
    Edge[label = $230$](S)(M)
    end{tikzpicture}
    end{center}

    end{document}


    enter image description here



    With arrows on edges



    documentclass[11pt]{scrartcl}
    usepackage{tkz-graph}


    begin{document}
    begin{tikzpicture}
    SetUpEdge[lw = 1.5pt,
    color = orange,
    labelcolor = white]
    GraphInit[vstyle=Normal]
    SetGraphUnit{3}
    tikzset{VertexStyle/.append style={fill}}
    Vertex{P}
    NOEA(P){B} SOEA(P){M} NOEA(B){D}
    SOEA(B){C} SOEA(C){L}
    tikzset{EdgeStyle/.style={->}}
    Edge[label=$3$](C)(B)
    Edge[label=$10$](D)(B)
    Edge[label=$10$](L)(M)
    Edge[label=$10$](B)(P)
    tikzset{EdgeStyle/.style={<->}}
    Edge[label=$4$](P)(M)
    Edge[label=$9$](C)(M)
    Edge[label=$4$](C)(L)
    Edge[label=$5$](C)(D)
    Edge[label=$10$](B)(M)
    tikzset{EdgeStyle/.style={<->,relative=false,in=0,out=60}}
    Edge[label=$11$](L)(D)
    end{tikzpicture}
    end{document}


    enter image description here



    update : Version with tikz and automata



    documentclass[11pt]{scrartcl} 
    PassOptionsToPackage{usenames,dvipsnames,svgnames}{xcolor}
    usepackage{tikz}
    usetikzlibrary{arrows,positioning,automata}

    begin{document}
    begin{tikzpicture}[>=stealth',shorten >=1pt,node distance=3cm,on grid,initial/.style ={}]
    node[state] (P) {$P$};
    node[state] (B) [above right =of P] {$B$};
    node[state] (M) [below right =of P] {$M$};
    node[state] (D) [above right =of B] {$D$};
    node[state] (C) [below right =of B] {$C$};
    node[state] (L) [below right =of C] {$L$};
    tikzset{mystyle/.style={->,double=orange}}
    tikzset{every node/.style={fill=white}}
    path (C) edge [mystyle] node {$3$} (B)
    (D) edge [mystyle] node {$10$} (B)
    (L) edge [mystyle] node {$10$} (M)
    (B) edge [mystyle] node {$10$} (P);
    tikzset{mystyle/.style={<->,double=orange}}
    path (P) edge [mystyle] node {$4$} (M)
    (C) edge [mystyle] node {$9$} (M)
    (C) edge [mystyle] node {$4$} (D)
    (B) edge [mystyle] node {$5$} (M);
    tikzset{mystyle/.style={<->,relative=false,in=0,out=60,double=orange}}
    path (L) edge [mystyle] node {$10$} (D);
    end{tikzpicture}
    end{document}


    enter image description here






    share|improve this answer















    I made tkz-graph and tkz-berge to help beginners to draw some graphs. tkz-berge is used for specials graphs (named graphs in graph theory)



    You can use only tikz to draw graphs.
    Version with tkz-graph



    documentclass{article}
    usepackage[utf8]{inputenc}
    usepackage[usenames,dvipsnames]{xcolor}
    usepackage{fullpage}
    usepackage[upright]{fourier}
    usepackage{tkz-graph}
    usetikzlibrary{arrows}
    thispagestyle{empty}
    begin{document}
    SetVertexNormal[Shape = circle,
    FillColor = orange,
    LineWidth = 2pt]
    SetUpEdge[lw = 1.5pt,
    color = black,
    labelcolor = white,
    labeltext = red,
    labelstyle = {sloped,draw,text=blue}]
    begin{center}
    begin{tikzpicture}
    Vertex[x=0 ,y=0]{K}
    Vertex[x=0 ,y=2]{F}
    Vertex[x=-1,y=4]{D}
    Vertex[x=3 ,y=7]{H}
    Vertex[x=8 ,y=5]{B}
    Vertex[x=9 ,y=2]{N}
    Vertex[x=5 ,y=0]{M}
    Vertex[x=3 ,y=1]{S}
    tikzset{EdgeStyle/.append style = {bend left}}
    Edge[label = $120$](K)(F)
    Edge[label = $650$](H)(S)
    Edge[label = $780$](H)(M)
    Edge[label = $490$](D)(B)
    Edge[label = $600$](D)(M)
    Edge[label = $580$](B)(M)
    Edge[label = $600$](H)(N)
    Edge[label = $490$](F)(H)
    tikzset{EdgeStyle/.append style = {bend right}}
    Edge[label = $630$](S)(B)
    Edge[label = $210$](S)(N)
    Edge[label = $230$](S)(M)
    end{tikzpicture}
    end{center}

    end{document}


    enter image description here



    With arrows on edges



    documentclass[11pt]{scrartcl}
    usepackage{tkz-graph}


    begin{document}
    begin{tikzpicture}
    SetUpEdge[lw = 1.5pt,
    color = orange,
    labelcolor = white]
    GraphInit[vstyle=Normal]
    SetGraphUnit{3}
    tikzset{VertexStyle/.append style={fill}}
    Vertex{P}
    NOEA(P){B} SOEA(P){M} NOEA(B){D}
    SOEA(B){C} SOEA(C){L}
    tikzset{EdgeStyle/.style={->}}
    Edge[label=$3$](C)(B)
    Edge[label=$10$](D)(B)
    Edge[label=$10$](L)(M)
    Edge[label=$10$](B)(P)
    tikzset{EdgeStyle/.style={<->}}
    Edge[label=$4$](P)(M)
    Edge[label=$9$](C)(M)
    Edge[label=$4$](C)(L)
    Edge[label=$5$](C)(D)
    Edge[label=$10$](B)(M)
    tikzset{EdgeStyle/.style={<->,relative=false,in=0,out=60}}
    Edge[label=$11$](L)(D)
    end{tikzpicture}
    end{document}


    enter image description here



    update : Version with tikz and automata



    documentclass[11pt]{scrartcl} 
    PassOptionsToPackage{usenames,dvipsnames,svgnames}{xcolor}
    usepackage{tikz}
    usetikzlibrary{arrows,positioning,automata}

    begin{document}
    begin{tikzpicture}[>=stealth',shorten >=1pt,node distance=3cm,on grid,initial/.style ={}]
    node[state] (P) {$P$};
    node[state] (B) [above right =of P] {$B$};
    node[state] (M) [below right =of P] {$M$};
    node[state] (D) [above right =of B] {$D$};
    node[state] (C) [below right =of B] {$C$};
    node[state] (L) [below right =of C] {$L$};
    tikzset{mystyle/.style={->,double=orange}}
    tikzset{every node/.style={fill=white}}
    path (C) edge [mystyle] node {$3$} (B)
    (D) edge [mystyle] node {$10$} (B)
    (L) edge [mystyle] node {$10$} (M)
    (B) edge [mystyle] node {$10$} (P);
    tikzset{mystyle/.style={<->,double=orange}}
    path (P) edge [mystyle] node {$4$} (M)
    (C) edge [mystyle] node {$9$} (M)
    (C) edge [mystyle] node {$4$} (D)
    (B) edge [mystyle] node {$5$} (M);
    tikzset{mystyle/.style={<->,relative=false,in=0,out=60,double=orange}}
    path (L) edge [mystyle] node {$10$} (D);
    end{tikzpicture}
    end{document}


    enter image description here







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Dec 10 '11 at 7:48

























    answered Dec 5 '11 at 14:16









    Alain MatthesAlain Matthes

    73.2k7162294




    73.2k7162294








    • 1





      It might be nice to include where these packages can be found (link) and on a personal note, is there any documentation in English? My French is not what it used to be in high school and even then it was pretty bad. Also, it may be better to add a directed example here, since the question specifically asks for it.

      – Roelof Spijker
      Dec 5 '11 at 14:26











    • TeXlive 2011, MikTeX or here ctan.org/tex-archive/macros/latex/contrib/tkz/tkz-graph. With the next version, there will be a French version. Count Zero will help me to translate ( tex.stackexchange.com/questions/35738/… ). You have some examples on my site altermundus.com/pages/tkz/graph/index.html

      – Alain Matthes
      Dec 5 '11 at 15:49













    • That's almost what I need and I have typeset a similar undirected weighted graph using "Prim's algorithm" example from texample.net. Although, I need to include somehow a direction for each edge in the graph. Frankly, the edges should be arrows pointing from a source vertex to a destination vertex rather than simply connecting the two.

      – user1049393
      Dec 6 '11 at 11:54











    • I added an example with arrows on edges. You need to study tikz to modify sometimes the style of edges or vertices.

      – Alain Matthes
      Dec 6 '11 at 14:48













    • Thanks for your input, Altermundus, but I cannot accept your answer as I have a lot of code purely in TikZ/PGF, and, thus, cannot afford porting to tkz-graph. Frankly, I ended up using 'automata' - TikZ-library in order to draw directed, weighted graphs (one, e.g., represented by the second image in your answer).

      – user1049393
      Dec 9 '11 at 18:00














    • 1





      It might be nice to include where these packages can be found (link) and on a personal note, is there any documentation in English? My French is not what it used to be in high school and even then it was pretty bad. Also, it may be better to add a directed example here, since the question specifically asks for it.

      – Roelof Spijker
      Dec 5 '11 at 14:26











    • TeXlive 2011, MikTeX or here ctan.org/tex-archive/macros/latex/contrib/tkz/tkz-graph. With the next version, there will be a French version. Count Zero will help me to translate ( tex.stackexchange.com/questions/35738/… ). You have some examples on my site altermundus.com/pages/tkz/graph/index.html

      – Alain Matthes
      Dec 5 '11 at 15:49













    • That's almost what I need and I have typeset a similar undirected weighted graph using "Prim's algorithm" example from texample.net. Although, I need to include somehow a direction for each edge in the graph. Frankly, the edges should be arrows pointing from a source vertex to a destination vertex rather than simply connecting the two.

      – user1049393
      Dec 6 '11 at 11:54











    • I added an example with arrows on edges. You need to study tikz to modify sometimes the style of edges or vertices.

      – Alain Matthes
      Dec 6 '11 at 14:48













    • Thanks for your input, Altermundus, but I cannot accept your answer as I have a lot of code purely in TikZ/PGF, and, thus, cannot afford porting to tkz-graph. Frankly, I ended up using 'automata' - TikZ-library in order to draw directed, weighted graphs (one, e.g., represented by the second image in your answer).

      – user1049393
      Dec 9 '11 at 18:00








    1




    1





    It might be nice to include where these packages can be found (link) and on a personal note, is there any documentation in English? My French is not what it used to be in high school and even then it was pretty bad. Also, it may be better to add a directed example here, since the question specifically asks for it.

    – Roelof Spijker
    Dec 5 '11 at 14:26





    It might be nice to include where these packages can be found (link) and on a personal note, is there any documentation in English? My French is not what it used to be in high school and even then it was pretty bad. Also, it may be better to add a directed example here, since the question specifically asks for it.

    – Roelof Spijker
    Dec 5 '11 at 14:26













    TeXlive 2011, MikTeX or here ctan.org/tex-archive/macros/latex/contrib/tkz/tkz-graph. With the next version, there will be a French version. Count Zero will help me to translate ( tex.stackexchange.com/questions/35738/… ). You have some examples on my site altermundus.com/pages/tkz/graph/index.html

    – Alain Matthes
    Dec 5 '11 at 15:49







    TeXlive 2011, MikTeX or here ctan.org/tex-archive/macros/latex/contrib/tkz/tkz-graph. With the next version, there will be a French version. Count Zero will help me to translate ( tex.stackexchange.com/questions/35738/… ). You have some examples on my site altermundus.com/pages/tkz/graph/index.html

    – Alain Matthes
    Dec 5 '11 at 15:49















    That's almost what I need and I have typeset a similar undirected weighted graph using "Prim's algorithm" example from texample.net. Although, I need to include somehow a direction for each edge in the graph. Frankly, the edges should be arrows pointing from a source vertex to a destination vertex rather than simply connecting the two.

    – user1049393
    Dec 6 '11 at 11:54





    That's almost what I need and I have typeset a similar undirected weighted graph using "Prim's algorithm" example from texample.net. Although, I need to include somehow a direction for each edge in the graph. Frankly, the edges should be arrows pointing from a source vertex to a destination vertex rather than simply connecting the two.

    – user1049393
    Dec 6 '11 at 11:54













    I added an example with arrows on edges. You need to study tikz to modify sometimes the style of edges or vertices.

    – Alain Matthes
    Dec 6 '11 at 14:48







    I added an example with arrows on edges. You need to study tikz to modify sometimes the style of edges or vertices.

    – Alain Matthes
    Dec 6 '11 at 14:48















    Thanks for your input, Altermundus, but I cannot accept your answer as I have a lot of code purely in TikZ/PGF, and, thus, cannot afford porting to tkz-graph. Frankly, I ended up using 'automata' - TikZ-library in order to draw directed, weighted graphs (one, e.g., represented by the second image in your answer).

    – user1049393
    Dec 9 '11 at 18:00





    Thanks for your input, Altermundus, but I cannot accept your answer as I have a lot of code purely in TikZ/PGF, and, thus, cannot afford porting to tkz-graph. Frankly, I ended up using 'automata' - TikZ-library in order to draw directed, weighted graphs (one, e.g., represented by the second image in your answer).

    – user1049393
    Dec 9 '11 at 18:00











    0














    Here is a stripped down version usking tkz-graph of Alain Matthes's code that provides reasonable looking (for CS paper) defaults.



    documentclass{article}
    usepackage{tkz-graph}

    begin{document}

    begin{figure}
    centering
    begin{tikzpicture}[scale=0.75,transform shape]
    Vertex[x=0,y=0]{K}
    Vertex[x=0,y=2]{F}
    Vertex[x=-1,y=4]{D}
    Vertex[x=3,y=7]{H}
    Vertex[x=8,y=5]{B}
    Vertex[x=9,y=2]{N}
    Vertex[x=5,y=0]{M}
    Vertex[x=3,y=3]{S}
    tikzstyle{LabelStyle}=[fill=white,sloped]
    tikzstyle{EdgeStyle}=
    Edge[label=$120$](K)(F)
    Edge[label=$650$](H)(S)
    Edge[label=$780$](H)(M)
    Edge[label=$490$](D)(B)
    Edge[label=$600$](D)(M)
    Edge[label=$580$](B)(M)
    Edge[label=$600$](H)(N)
    Edge[label=$490$](F)(H)
    tikzstyle{EdgeStyle}=[pre, bend right]
    Edge[label=$210$](S)(N)
    Edge[label=$230$](S)(M)
    end{tikzpicture}
    end{figure}

    end{document}


    example





    share




























      0














      Here is a stripped down version usking tkz-graph of Alain Matthes's code that provides reasonable looking (for CS paper) defaults.



      documentclass{article}
      usepackage{tkz-graph}

      begin{document}

      begin{figure}
      centering
      begin{tikzpicture}[scale=0.75,transform shape]
      Vertex[x=0,y=0]{K}
      Vertex[x=0,y=2]{F}
      Vertex[x=-1,y=4]{D}
      Vertex[x=3,y=7]{H}
      Vertex[x=8,y=5]{B}
      Vertex[x=9,y=2]{N}
      Vertex[x=5,y=0]{M}
      Vertex[x=3,y=3]{S}
      tikzstyle{LabelStyle}=[fill=white,sloped]
      tikzstyle{EdgeStyle}=
      Edge[label=$120$](K)(F)
      Edge[label=$650$](H)(S)
      Edge[label=$780$](H)(M)
      Edge[label=$490$](D)(B)
      Edge[label=$600$](D)(M)
      Edge[label=$580$](B)(M)
      Edge[label=$600$](H)(N)
      Edge[label=$490$](F)(H)
      tikzstyle{EdgeStyle}=[pre, bend right]
      Edge[label=$210$](S)(N)
      Edge[label=$230$](S)(M)
      end{tikzpicture}
      end{figure}

      end{document}


      example





      share


























        0












        0








        0







        Here is a stripped down version usking tkz-graph of Alain Matthes's code that provides reasonable looking (for CS paper) defaults.



        documentclass{article}
        usepackage{tkz-graph}

        begin{document}

        begin{figure}
        centering
        begin{tikzpicture}[scale=0.75,transform shape]
        Vertex[x=0,y=0]{K}
        Vertex[x=0,y=2]{F}
        Vertex[x=-1,y=4]{D}
        Vertex[x=3,y=7]{H}
        Vertex[x=8,y=5]{B}
        Vertex[x=9,y=2]{N}
        Vertex[x=5,y=0]{M}
        Vertex[x=3,y=3]{S}
        tikzstyle{LabelStyle}=[fill=white,sloped]
        tikzstyle{EdgeStyle}=
        Edge[label=$120$](K)(F)
        Edge[label=$650$](H)(S)
        Edge[label=$780$](H)(M)
        Edge[label=$490$](D)(B)
        Edge[label=$600$](D)(M)
        Edge[label=$580$](B)(M)
        Edge[label=$600$](H)(N)
        Edge[label=$490$](F)(H)
        tikzstyle{EdgeStyle}=[pre, bend right]
        Edge[label=$210$](S)(N)
        Edge[label=$230$](S)(M)
        end{tikzpicture}
        end{figure}

        end{document}


        example





        share













        Here is a stripped down version usking tkz-graph of Alain Matthes's code that provides reasonable looking (for CS paper) defaults.



        documentclass{article}
        usepackage{tkz-graph}

        begin{document}

        begin{figure}
        centering
        begin{tikzpicture}[scale=0.75,transform shape]
        Vertex[x=0,y=0]{K}
        Vertex[x=0,y=2]{F}
        Vertex[x=-1,y=4]{D}
        Vertex[x=3,y=7]{H}
        Vertex[x=8,y=5]{B}
        Vertex[x=9,y=2]{N}
        Vertex[x=5,y=0]{M}
        Vertex[x=3,y=3]{S}
        tikzstyle{LabelStyle}=[fill=white,sloped]
        tikzstyle{EdgeStyle}=
        Edge[label=$120$](K)(F)
        Edge[label=$650$](H)(S)
        Edge[label=$780$](H)(M)
        Edge[label=$490$](D)(B)
        Edge[label=$600$](D)(M)
        Edge[label=$580$](B)(M)
        Edge[label=$600$](H)(N)
        Edge[label=$490$](F)(H)
        tikzstyle{EdgeStyle}=[pre, bend right]
        Edge[label=$210$](S)(N)
        Edge[label=$230$](S)(M)
        end{tikzpicture}
        end{figure}

        end{document}


        example






        share











        share


        share










        answered 2 mins ago









        qwrqwr

        1144




        1144






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to TeX - LaTeX Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f37185%2ftypesetting-a-directed-weighted-graph-with-tikz%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Accessing regular linux commands in Huawei's Dopra Linux

            Can't connect RFCOMM socket: Host is down

            Kernel panic - not syncing: Fatal Exception in Interrupt