Зміст курсу
Algorithms and Data Structures Overview
Algorithms and Data Structures Overview
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)
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)
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.
Дякуємо за ваш відгук!