Junior — Middle
Can you explain what a graph is in the context of computer science and data?
sobes.tech AI
Answer from AI
In computer science, a graph is a data structure consisting of a set of nodes (vertices) and connections between them (edges). Graphs are used to model relationships and connections between objects, such as social networks, routes on a map, dependencies between tasks.
A graph can be directed (edges have a direction) or undirected. Edges can also have weights, reflecting cost or distance.
An example of a simple graph in Python using a dictionary:
graph = {
'A': ['B', 'C'],
'B': ['A', 'D'],
'C': ['A', 'D'],
'D': ['B', 'C']
}
Here, vertex 'A' is connected to 'B' and 'C', and so on.