Beispiel #1
0
        private void dfs(EdgeWeightDiGraph g, int begin)
        {
            isMarked[begin] = true;
            inStack[begin]  = true;
            foreach (DirectedEdge e in g.getAdj(begin))
            {
                int node = e.getTo();
                //if (hasCycle()) return;

                if (!isMarked[node])
                {
                    edgeTo[node] = begin;
                    dfs(g, node);
                }
                else if (inStack[node])
                { // 如果当前路径Stack中含有node,又再次访问的话,说明有环
                  // 将环保存下来
                    Stack <Vertex> cycle = new Stack <Vertex>();
                    for (int i = begin; i != node; i = edgeTo[i])
                    {
                        cycle.Push(g.getVetrx(i));
                        Console.Write("访问环点:" + i + " ");
                    }
                    cycle.Push(g.getVetrx(node));
                    Console.Write("访问环点:" + node + " ");
                    //   cycle.Push(g.getVetrx(begin));

                    cycleList.Add(cycle);
                }
            }
            inStack[begin] = false;
        }
Beispiel #2
0
 /// <summary>
 /// 设计思路:
 /// 找出所有连接电池的环
 /// </summary>
 /// <param name="g"></param>
 /// <param name="battery"></param>
 public void Generation(EdgeWeightDiGraph g, Vertex battery)
 {//计算电路,为了方便直接给出电池在哪.为了方便暂定电池只有一个
     if (CheckLinked(battery))
     {
         Voltage = battery.Voltage; //电池的电压为总电压
         if (cycleList.Count == 1)
         {                          //串联
             Console.Write("电路为串联");
             for (int i = 0; i < cycleList[0].Count - 1; i++)
             {
                 AllResistance += cycleList[0].ElementAt(i).Resistance;
             }
             if (AllResistance == 0)
             {
                 Console.WriteLine("短路了");
                 return;
             }
             Eletry = Voltage / AllResistance;
             SetEle(g, cycleList[0], Eletry);
             Console.WriteLine("总电阻为:" + AllResistance);
         }
         else
         {
             Console.Write("并联,算法测试中……可能有bug");
             for (int i = 0; i < cycleList.Count; i++)
             {
             }
         }
     }
 }
Beispiel #3
0
 public DirectedCycle(EdgeWeightDiGraph g)
 {
     inStack   = new bool[g.getV()];
     edgeTo    = new int[g.getV()];
     isMarked  = new bool[g.getV()];
     cycleList = new List <Stack <Vertex> >();
     for (int i = 0; i < g.getV(); i++)
     {
         if (!isMarked[i])
         {
             dfs(g, i);
         }
     }
 }
Beispiel #4
0
        static void Main(string[] args)
        {
            Console.WriteLine("先试试串联");
            Console.WriteLine("请输入顶点数");
            int vertxCount = int.Parse(Console.ReadLine());

            Console.WriteLine("请输入边数");
            int edgeCount       = int.Parse(Console.ReadLine());
            EdgeWeightDiGraph g = new EdgeWeightDiGraph(vertxCount);

            for (int v = 0; v < vertxCount; v++)
            {
                if (v != 0)
                {
                    g.getVetrx(v).Resistance = 5;//电阻暂定为5
                }
            }
            for (int i = 0; i < edgeCount; i++)
            {
                Console.Write("输入边顶点1:");
                int v = int.Parse(Console.ReadLine());
                Console.Write("输入边顶点2:");
                int          w = int.Parse(Console.ReadLine());
                DirectedEdge e = new DirectedEdge(v, w);
                g.addEdge(e);
            }
            g.getVetrx(0).Voltage = 20;//设为电池,电压为20V
            DirectedCycle checkCircle = new DirectedCycle(g);

            checkCircle.Generation(g, g.getVetrx(0));
            if (checkCircle.hasCycle())
            {
                Console.WriteLine(checkCircle.toString());
            }
            else
            {
                Console.WriteLine("无环,断路");
            }
            Console.WriteLine(g.toString());
            Console.ReadKey();
        }
Beispiel #5
0
 public void SetEle(EdgeWeightDiGraph g, Stack <Vertex> cycle, float electry)//每圈分别设置电压,电流
 {
     for (int c = 0; c < cycle.Count - 1; c++)
     {
         for (int i = 0; i < g.getV(); i++)
         {
             if (g.getVetrx(i) == cycle.ElementAt(c))
             {
                 g.getVetrx(i).Electricity = electry;
                 if (g.getVetrx(i).Resistance != 0)//不是电线或者电池的情况下计算电压。
                 {
                     g.getVetrx(i).Voltage = electry * g.getVetrx(i).Resistance;
                 }
                 Console.WriteLine("发电中,元器件" + i + "电流为:" + electry + "电压为:" + g.getVetrx(i).Voltage);
                 for (int j = 0; j < g.getVetrx(i).adj.Count; j++)
                 {
                     g.getVetrx(i).adj[j].Electricity = electry;
                 }
             }
         }
     }
 }