Docsity

Algorithms and Data Structures: Graphs, Esquemas y mapas conceptuales de Matemáticas

Vista previa parcial del texto

¡Descarga Algorithms and Data Structures: Graphs y más Esquemas y mapas conceptuales en PDF de Matemáticas solo en Docsity!

ALGORITHMS AND DATA STRUCTURES

Alex Martínez Mingo IE School of Science and Technology Core Subject Academic year 2023/

Algorithms and

Data Structures:

Graphs

  1. Introduction to graphs
  2. Graphs vs Trees
  3. Traversal algorithms
  4. Breadth-first search
  5. Implementing the graph
  6. Depth-first search

Introduction

This session introduces graphs. First, we will talk about what graphs are. Then we will see our first graph algorithm, the breadth-first search algorithm.

Breadth-first search allows you to find the shortest distance between two things. But shortest distance can mean a lot of things!

Graphs. Intro

A graph models a set of connections. For example, suppose you and your friends are playing poker, and you want to model who owes whom money. Here is how you could say, “Alex owes Rama money”:

Graphs. Parts

Alex owes Rama money, Tom owes Adit money, and so on. Each graph is made up of nodes and edges.

Graphs. Parts

Graphs are made up of nodes (also called vertices) and edges. A node can be directly connected to many other nodes. Those nodes are called its neighbors. In this graph, Rama is Alex’s neighbor. Adit is not Alex neighbor, because they are not directly connected. But Adit is Rama’s and Tom’s neighbor.

Graph.

BDBA Class

Graph.

BBADBA Class

Graphs vs Trees

Linear data structures such as linked lists and arrays have a logical start and logical end. One direction and one way. In a linear data structure, data elements are sequentially connected and each element is traversable through a single run.

Graphs and Trees are non linear data structures. A non-linear data structure has no set sequence of connecting all its elements. Such data structures supports multi-level storage and often cannot be traversed in single run. Such data structures are not easy to implement but are more efficient in using computer memory. Examples of non-linear data structures are Trees, BST, Graphs etc..

Graphs vs Trees

Okay, graphs and trees are nonlinear collections of linked nodes, in fact, in previous sessions we said that a tree is a constrained graph and that’s because a tree is simply a hierarchical graph with a root node. Let’s see their differences:

Graph Tree

Graphs vs Trees

Directed or Undirected:

● Tree data structures they are assumed to be directed whereas graphs can be directed or undirected. In directed structures the nodes have 'arrows' pointing to other nodes, the relationship is in one way. An undirected graph doesn’t have any arrows, and both nodes are each other’s neighbors. For example, both of these graphs are equal:

Graphs vs Trees

Common trees:

● Binary Trees, Binary Search Trees, Heaps, AVL Trees and Red Black Trees

Common graphs:

● Simple graphs, Multigraphs, Directed graphs, Undirected graphs and Weighted graphs.

Intro

Do you remember the traversal algorithms? To iterate over the nodes of a non-linear structure we have several ways to do it.

In the trees and in the graphs we have to use these algorithms, although they are a little different in each structure.

Traversal Algorithms. Trees

Tree traversals are just a special type of graph traversal. Tree data structures can be traversed in In-Order, Pre-Order and Post-Order (DFS) or BFS traversal algorithms.

● In binary search trees , In-Order traversals return nodes in non-decreasing order. ● Pre-Order traversals are used to create a copy of the tree. ● Post-Order traversals are used to delete the tree