algorithm


Improvements requested by nbro:

  • Other: I think that most of these algorithms, which are very important, deserve its own section. For once where there are algorithms that deserve their own section, you put them all together: incredible! - Sep 11 at 15:04

This draft deletes the entire topic.

expand all collapse all

Examples

  • 1

    Graph Theory is the study of graphs, which are mathematical structures used to model pairwise relations between objects.

    Did you know, almost all the problems of planet Earth can be converted into problems of Roads and Cities, and solved? Graph Theory was invented many years ago, even before the invention of computer. Leonhard Euler wrote a paper on the Seven Bridges of Königsberg which is regarded as the first paper of Graph Theory. Since then, people have come to realize that if we can convert any problem to this City-Road problem, we can solve it easily by Graph Theory.

    Graph Theory has many applications.One of the most common application is to find the shortest distance between one city to another. We all know that to reach your PC, this web-page had to travel many routers from the server. Graph Theory helps it to find out the routers that needed to be crossed. During war, which street needs to be bombarded to disconnect the capital city from others, that too can be found out using Graph Theory.

    Let us first learn some basic definitions on Graph Theory.

    Graph:

    Let's say, we have 6 cities. We mark them as 1, 2, 3, 4, 5, 6. Now we connect the cities that have roads between each other.

    Connection between cities

    This is a simple graph where some cities are shown with the roads that are connecting them. In Graph Theory, we call each of these cities Node or Vertex and the roads are called Edge. Graph is simply a connection of these nodes and edges.

    A node can represent a lot of things. In some graphs, nodes represent cities, some represent airports, some represent a square in a chessboard. Edge represents the relation between each nodes. That relation can be the time to go from one airport to another, the moves of a knight from one square to all the other squares etc.

    Moves of Knight from a single point

                                                 Path of Knight in a Chessboard

    In simple words, a Node represents any object and Edge represents the relation between two objects.

    Adjacent Node:

    If a node A shares an edge with node B, then B is considered to be adjacent to A. In other words, if two nodes are directly connected, they are called adjacent nodes. One node can have multiple adjacent nodes.

    Directed and Undirected Graph:

    In directed graphs, the edges have direction signs on one side, that means the edges are Unidirectional. On the other hand, the edges of undirected graphs have direction signs on both sides, that means they are Bidirectional. Usually undirected graphs are represented with no signs on the either sides of the edges.

    Let's assume there is a party going on. The people in the party are represented by nodes and there is an edge between two people if they shake hands. Then this graph is undirected because any person A shake hands with person B if and only if B also shakes hands with A. In contrast, if the edges from a person A to another person B corresponds to A's admiring B, then this graph is directed, because admiration is not necessarily reciprocated. The former type of graph is called an undirected graph and the edges are called undirected edges while the latter type of graph is called a directed graph and the edges are called directed edges.

    Weighted and Unweighted Graph:

    A weighted graph is a graph in which a number (the weight) is assigned to each edge. Such weights might represent for example costs, lengths or capacities, depending on the problem at hand. Weighted Graph

    An unweighted graph is simply the opposite. We assume that, the weight of all the edges are same (presumably 1).

    Path:

    A path represents a way of going from one node to another. It consists of sequence of edges. There can be multiple paths between two nodes. Path Graph

    In the example above, there are two paths from A to D. A->B, B->C, C->D is one path. The cost of this path is 3 + 4 + 2 = 9. Again, there's another path A->D. The cost of this path is 10. The path that costs the lowest is called shortest path.

    Degree:

    The degree of a vertex is the number of edges that are connected to it. If there's any edge that connects to the vertex at both ends (a loop) is counted twice.

    In directed graphs, the nodes have two types of degrees:

    • In-degree: The number of edges that point to the node.
    • Out-degree: The number of edges that point from the node to other nodes.

    For undirected graphs, they are simply called degree.

    Degrees of a graph

    Some Algorithms Related to Graph Theory

    • Bellman–Ford algorithm
    • Dijkstra's algorithm
    • Ford–Fulkerson algorithm
    • Kruskal's algorithm
    • Nearest neighbour algorithm
    • Prim's algorithm
    • Depth-first search
    • Breadth-first search
  • 1

    Adjacency list is a collection of unordered lists used to represent a finite graph. Each list describes the set of neighbors of a vertex in a graph. It takes less memory to store graphs.

    Let's see a graph, and its adjacency matrix:Adjacency Matrix and It's Graph

    Now we create a list using these values.

    Adjacency List

    This is called adjacency list. It shows which nodes are connected to which nodes. We can store this information using a 2D array. But will cost us the same memory as Adjacency Matrix. Instead we are going to use dynamically allocated memory to store this one.

    Many languages support Vector or List which we can use to store adjacency list. For these, we don't need to specify the size of the List. We only need to specify the maximum number of nodes.

    The pseudo-code will be:

    Procedure Adjacency-List(maxN, E):       // maxN denotes the maximum number of nodes
    edge[maxN] = Vector()                    // E denotes the number of edges
    for i from 1 to E
        input -> x, y                        // Here x, y denotes there is an edge between x, y
        edge[x].push(y)
        edge[y].push(x)
    end for
    Return edge
    

    Since this one is an undirected graph, it there is an edge from x to y, there is also an edge from y to x. If it was a directed graph, we'd omit the second one. For weighted graphs, we need to store the cost too. We'll create another vector or list named cost[] to store these. The pseudo-code:

    Procedure Adjacency-List(maxN, E):
    edge[maxN] = Vector()
    cost[maxN] = Vector()
    for i from 1 to E
        input -> x, y, w
        edge[x].push(y)
        cost[x].push(w)
    end for
    Return edge, cost
    

    From this one, we can easily find out the total number of nodes connected to any node, and what these nodes are. It takes less time than Adjacency Matrix. But if we needed to find out if there's an edge between u and v, it'd have been easier if we kept an adjacency matrix.

  • 1

    To store a graph, two methods are common:

    • Adjacency Matrix
    • Adjacency List

    An adjacency matrix is a square matrix used to represent a finite graph. The elements of the matrix indicate whether pairs of vertices are adjacent or not in the graph.

    Adjacent means 'next to or adjoining something else' or to be beside something. For example, your neighbors are adjacent to you. In graph theory, if we can go to node B from node A, we can say that node B is adjacent to node A. Now we will learn about how to store which nodes are adjacent to which one via Adjacency Matrix. This means, we will represent which nodes share edge between them. Here matrix means 2D array.

    Graph and Adjacency Matrix

    Here you can see a table beside the graph, this is our adjacency matrix. Here Matrix[i][j] = 1 represents there is an edge between i and j. If there's no edge, we simply put Matrix[i][j] = 0.

    These edges can be weighted, like it can represent the distance between two cities. Then we'll put the value in Matrix[i][j] instead of putting 1.

    The graph described above is Bidirectional or Undirected, that means, if we can go to node 1 from node 2, we can also go to node 2 from node 1. If the graph was Directed, then there would've been arrow sign on one side of the graph. Even then, we could represent it using adjacency matrix.

    Adjacency Matrix of Directed Weighted Graph

    We represent the nodes that don't share edge by infinity. One thing to be noticed is that, if the graph is undirected, the matrix becomes symmetric.

    The pseudo-code to create the matrix:

    Procedure AdjacencyMatrix(N):    //N represents the number of nodes
    Matrix[N][N]
    for i from 1 to N
        for j from 1 to N
            Take input -> Matrix[i][j]
        endfor
    endfor
    

    We can also populate the Matrix using this common way:

    Procedure AdjacencyMatrix(N, E):    // N -> number of nodes
    Matrix[N][E]                        // E -> number of edges
    for i from 1 to E
        input -> n1, n2, cost
        Matrix[n1][n2] = cost
        Matrix[n2][n1] = cost
    endfor
    

    For directed graphs, we can remove Matrix[n2][n1] = cost line.

    The drawbacks of using Adjacency Matrix:

    Memory is a huge problem. No matter how many edges are there, we will always need N * N sized matrix where N is the number of nodes. If there are 10000 nodes, the matrix size will be 4 * 10000 * 10000 around 381 megabytes. This is a huge waste of memory if we consider graphs that have a few edges.

    Suppose we want to find out to which node we can go from a node u. We'll need to check the whole row of u, which costs a lot of time.

    The only benefit is that, we can easily find the connection between u-v nodes, and their cost using Adjacency Matrix.

I am downvoting this example because it is...

Syntax

Syntax

Parameters

Parameters

Remarks

Graphs are a mathematical structure that model sets of objects that may or may not be connected with members from sets of edges or links.

A graph can be described through two different sets of mathematical objects:

  • A set of vertices.
  • A set of edges that connect pairs of vertices.

Graphs can be either directed or undirected.

  • Directed graphs contain edges that "connect" only one way.
  • Undirected graphs contain only edges that automatically connect two vertices together in both directions.
Still have a question about Graphs? Ask Question

Topic Outline