Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lära What is Graph | Graphs
Algorithms and Data Structures Overview

book
What is Graph

A graph is a mathematical structure consisting of two sets called a set of vertices and a set of edges. The graph is denoted as G(V, E). Of course, vertices in a graph are connected by edges.

The edges and vertices of a graph can represent different entities. For example, in social networks, the vertices of a graph can represent people, and edges can represent relations between those people.

from lolviz import *
from IPython.display import display_png

graph = graphviz.Digraph()

graph.node('Will')
graph.node('Sam')
graph.node('Mike')
graph.node('Jack')
graph.node('Diana')
graph.node('Nadia')

graph.edge('Will', 'Sam', label='follows')
graph.edge('Will', 'Mike', label='follows')
graph.edge('Will', 'Diana', label='follows')
graph.edge('Sam', 'Mike', label='follows')
graph.edge('Sam', 'Diana', label='follows')
graph.edge('Mike', 'Will', label='follows')
graph.edge('Mike', 'Sam', label='follows')
graph.edge('Mike', 'Nadia', label='follows')
graph.edge('Jack', 'Will', label='follows')
graph.edge('Jack', 'Nadia', label='follows')
graph.edge('Diana', 'Will', label='follows')
graph.edge('Diana', 'Jack', label='follows')
graph.edge('Diana', 'Nadia', label='follows')
graph.edge('Nadia', 'Diana', label='follows')
graph.edge('Nadia', 'Jack', label='follows')
graph.edge('Nadia', 'Sam', label='follows')

display_png(graph)
123456789101112131415161718192021222324252627282930
from lolviz import * from IPython.display import display_png graph = graphviz.Digraph() graph.node('Will') graph.node('Sam') graph.node('Mike') graph.node('Jack') graph.node('Diana') graph.node('Nadia') graph.edge('Will', 'Sam', label='follows') graph.edge('Will', 'Mike', label='follows') graph.edge('Will', 'Diana', label='follows') graph.edge('Sam', 'Mike', label='follows') graph.edge('Sam', 'Diana', label='follows') graph.edge('Mike', 'Will', label='follows') graph.edge('Mike', 'Sam', label='follows') graph.edge('Mike', 'Nadia', label='follows') graph.edge('Jack', 'Will', label='follows') graph.edge('Jack', 'Nadia', label='follows') graph.edge('Diana', 'Will', label='follows') graph.edge('Diana', 'Jack', label='follows') graph.edge('Diana', 'Nadia', label='follows') graph.edge('Nadia', 'Diana', label='follows') graph.edge('Nadia', 'Jack', label='follows') graph.edge('Nadia', 'Sam', label='follows') display_png(graph)
copy

On the other hand, the vertices can represent street crossings, and the edges can represent the streets.

from lolviz import *
from IPython.display import display_png

graph = graphviz.Graph()

graph.node('Catalonia Square')
graph.node('University Square')
graph.node('Spain Square')
graph.node('Drassanes Square')

graph.edge('Catalonia Square', 'University Square', label='University Roundabout')
graph.edge('University Square', 'Spain Square', label='Gran Via de les Corts Catalanes')
graph.edge('Spain Square', 'Drassanes Square', label='Parallel Avenue')
graph.edge('Drassanes Square', 'Catalonia Square', label='La Rambla')

display_png(graph)
12345678910111213141516
from lolviz import * from IPython.display import display_png graph = graphviz.Graph() graph.node('Catalonia Square') graph.node('University Square') graph.node('Spain Square') graph.node('Drassanes Square') graph.edge('Catalonia Square', 'University Square', label='University Roundabout') graph.edge('University Square', 'Spain Square', label='Gran Via de les Corts Catalanes') graph.edge('Spain Square', 'Drassanes Square', label='Parallel Avenue') graph.edge('Drassanes Square', 'Catalonia Square', label='La Rambla') display_png(graph)
copy

Also, we can assign a weight to an edge of a graph that will denote the cost of passing from one vertex to another by this edge.

As you saw above, graphs can be directed and undirected. It depends on the nature of the underlying task and which type of graph we should choose.

question mark

What is a graph in the context of graph theory?

Select the correct answer

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 4. Kapitel 1

Fråga AI

expand
ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

some-alt