Skip to content

Commit 64da622

Browse files
nurdtechie98Chaitya62
authored andcommitted
Add Intro and Readme (#4)
* Add Intro * Intro Fixes * Readme Update * Markdown Fixes for readme
1 parent 3c0bd98 commit 64da622

File tree

3 files changed

+66
-0
lines changed

3 files changed

+66
-0
lines changed

Intro2Graphs.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
## Introduction
2+
Graphs are one of the fundamental data structures in the world of programming that find themselves at the core of a range of various types of problems that otherwise may be tedius to solve.
3+
4+
They can range in difficulty from finding a path on a 2D grid from a start location to an end location, to something as hard as finding the maximum amount of water that you can route through a set of pipes, each of which has a maximum capacity.Knowing the correct data structures to use with graph problems is critical. A problem that appears intractable may prove to be a few lines with the proper data structure.
5+
6+
## Graph terms
7+
Graphs can represent many different types of systems, from a two-dimensional grid,to a map of the internet that shows how long it takes data to move from computer A to computer B. We first need to define what components a graph consists of.
8+
In fact there are only two, nodes and edges.
9+
* A **node** (or vertex) is a discrete position in the graph.
10+
* An **edge** (or connection) is a link between two vertices that can be either directed or undirected and may have a cost associated with it.
11+
* An **undirected edge** means that there is no restriction on the direction you can travel along the edge.
12+
So for example, if there were an undirected edge from A to B you could move from A to B or from B to A.
13+
* A **directed edge** only allows travel in one direction, so if there were a directed edge from A to B you could travel from A to B, but not from B to A.
14+
An easy way to think about edges and vertices is that edges are a function of two vertices that returns a cost.
15+
> For those that are used to the mathematical description of graphs <br>
16+
> A graph G = {V, E} is defined as a set of vertices, V, and a collection of edges (which is not necessarily a set), E.
17+
> An edge can then be defined as (u, v) where u and v are elements of V.
18+
19+
## Representing a graph
20+
21+
### List based representation
22+
A tree only allows a node to have children, and there cannot be any loops in the tree, with a more general graph we can represent many different situations. A very common example used is flight paths between cities. If there is a flight between city A and city B there is an edge between the cities. The cost of the edge can be the length of time that it takes for the flight, or perhaps the amount of fuel used.
23+
24+
The way that we will represent this is to have a concept of a node (or vertex) that contains links to other nodes, and the data associated with that node. So for our flight path example we might have the name of the airport as the node data, and for every flight leaving that city we have an element in neighbors that points to the destination.
25+
Now a basic list based represenation of graph can be done in the following schema:
26+
```
27+
structure node
28+
[list of nodes] neighbors
29+
[data]
30+
end
31+
32+
cost(X, Y) := if (X.neighbors contains Y) return X.neighbors[Y];
33+
else "Not possible"
34+
35+
list nodes;
36+
```
37+
This is a very general way to represent a graph. It allows us to have multiple edges from one node to another and it is a very compact representation of a graph as well. However the downside is that it is usually more difficult to work
38+
39+
### Array Based representation
40+
Representing a graph as a list of nodes is a very flexible method. But usually on topcoder we have limits on the problems that attempt to make life easier for us. Normally our graphs are relatively small, with a small number of nodes and edges. When this is the case we can use a different type of data structure that is easier to work with.
41+
42+
The basic concept is to have a 2 dimensional array of integers, where the element in row i, at column j represents the edge cost from node i to j. If the connection from i to j is not possible, we use some sort of sentinel value (usually a very large or small value, like -1 or the maximum integer). Another nice thing about this type of structure is that we can represent directed or undirected edges very easily.
43+
44+
So for example, the following connection matrix:
45+
```
46+
A B C
47+
A 0 1 5
48+
B -1 0 1
49+
C -1 -1 0
50+
```
51+
would mean that node A has a 0 weight connection to itself, a 1 weight connection to node B and 5 weight connection to node C. Node B on the other hand has no connection to node A, a 0 weight connection to itself, and a 1 weight connection to C. Node C is connected to nobody.
52+
53+
This representation is very convenient for graphs that do not have multiple edges between each node, and allows us to simplify working with the graph.

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,14 @@
1+
<div style="text-align:center"><img src ="https://avatars2.githubusercontent.com/u/25126113?s=200&v=4" /></div>
2+
13
# Graph-notes
4+
5+
The following is a documentation to *Graphs and their algorithm* created by **Team Codecell :computer:** as a handout for the workshop conducted on the same topic.
6+
7+
We hope it will help you understand graphs better and make it possible for you to apply them with ease as and when required :fire:
8+
## Content
9+
* [Introduction](./Intro2Graphs.md)
10+
* [Algorithms](./Algorithms)
11+
* [Dijkstra](./Algorithms/dijkstra.cpp)
12+
* [Floyd Warshall](./Algorithms/Floyd_Warshall.cpp)
13+
* [Problems](./Problems)
14+

0 commit comments

Comments
 (0)